0

I have problem creating a console vb.net program that executes a stored procedure in Oracle database. I can do this if the stored procedure will be hard coded in the code. The problem is, the stored procedure will need to be place in a parameter from vendor program. Currently, we use a VB6 program to do this. Now, they want to convert this vb6 program into .NET but I don't know how to do it. I tried the approach below but it doesn't pass the command parameter from the 3rd party program.

Example. This is the screenshot of the 3rd party program:

EXTERNAL PROGRAM

The PLSQLSUB.exe program is a vb6 that invokes stored procedure CNC_UNLOCK_USERS.

This is the VB6 code, this is currently working but I don't know how to pass the external parameter in VB.NET

Private Sub Form_Load()
  Dim cnTrecs As New ADODB.Connection
  Dim QY As New ADODB.Command

  Dim I As Integer

  Dim Procedures(10) As String

  Dim ColonPos As Integer

  Dim CommLine As String
  Dim CommLength As Integer
  Dim StartPos As Integer
  Dim FileName As String
  Dim CheckForFile As String
  Dim User As String
  Dim Pass As String
  Dim Userid As String
  Dim Password As String

  Dim PLSCount As Integer

  PLSCount = 0

  CommLine = Command()
  CommLength = Len(Trim(CommLine))

  If Trim(CommLine) <> "" Then
    StartPos = 1
    For I = 1 To 10
      ColonPos = InStr(StartPos, Trim(CommLine), ";")

      If ColonPos > 0 Then
        Procedures(I) = Mid(CommLine, StartPos, ColonPos - StartPos)
        PLSCount = PLSCount + 1
        StartPos = ColonPos + 1
      ElseIf I > 1 Then
        If (CommLength - StartPos) > 0 Then
          Procedures(I) = Mid(CommLine, StartPos, CommLength - StartPos)
          MsgBox (Procedures(I))
          PLSCount = PLSCount + 1
          Exit For
        Else
          Exit For
        End If
      Else
        Procedures(I) = Trim(CommLine)
        PLSCount = PLSCount + 1
        Exit For
      End If
    Next
    Exit Sub
  Else
    Unload Me
    Exit Sub
  End If

  CheckForFile = Dir("C:\VB\vbtext1.txt")
  If CheckForFile <> "" Then FileName = "d:\VB\vbtext1.txt"
    Open FileName For Input As #5   ' Open UserID and Password file.
      Line Input #5, Userid
      Line Input #5, Password
    Close #5
  End If
  User = Mid(Userid, 1) ' Set database userID
  Pass = Mid(Password, 1) ' Set database password

  cnTrecs.Open "DSN=PLSQLSUB;" _
                & "Uid=" _
                & Trim$(User) & ";PWD=" _
                & Trim$(Pass)

  For I = 1 To PLSCount ' Loop until end of plsql procedures
    Print #1, "The Stored Procedure " & Procedures(I) & " was submitted for execution     
on/at "; Format(Now, "dd-mmm-yyyy hh:mm:ss")

    Set QY.ActiveConnection = cnTrecs
    QY.CommandType = adCmdStoredProc
    QY.CommandText = Procedures(I)
    QY.Execute
    Print #1, "The Stored Procedure " & Procedures(I) & " completed execution on/at ";   
    Format(Now, "dd-mmm-yyyy hh:mm:ss")
    Print #1, " "
  Next I

  If cnTrecs.State = adStateOpen Then
    cnTrecs.Close
  End If
End Sub

A friend told me about using the below code but I could not able to pass the stored procedure. Any help will be greatly appreciated.

Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs

For i As Integer = 0 To CommandLineArgs.Count - 1
   MessageBox.Show("The stored procedure is: " + CommandLineArgs(i))
Next
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • The above VB6 code produces some output, which you would like to capture in VB.NET - is that correct? – Victor Zakharov Jan 12 '13 at 17:27
  • That is correct. I want VB.NET to behave the same way, which is running a stored procedure from a parameter command (base from the screenshot). – Allan Tolentino Jan 12 '13 at 19:13
  • After I formatted the code - it became clear that everything starting at `CheckForFile = Dir("C:\VB\vbtext1.txt")` never runs. This is because `If Trim(CommLine) <> "" Then` clause calls `Exit Sub` on both execution paths. It appears the above VB6 code never used to work. Correct me if I am wrong. – Victor Zakharov Jan 12 '13 at 23:19
  • The vb6 code currently work. I did not paste the entire code, i just pasted the one i need to convert in .NET which is passing the command line or parameter. – Allan Tolentino Jan 12 '13 at 23:29
  • @AllanTolentino put a breakpoint in your .net code at your `for` statement. Look at what `CommandLineArgs` contains and please add the results to your question. – Mark Hall Jan 12 '13 at 23:33
  • @AllanTolentino: it looks like your VB6 is **consuming** command line parameters and not passing them. In this case use Mark's `Environment.GetCommandLineArgs` or my method with Sub Main. – Victor Zakharov Jan 12 '13 at 23:36

2 Answers2

0

If I understood you correctly, you want to convert existing VB6 to VB.NET, i.e. run a stored procedure from VB.NET. Because your SP does not have parameters, use below code:

Using conn As New OracleConnection("Server=YourOracle;Uid=uid;Pwd=pwd")
  conn.Open()
  Dim cmd As New OracleCommand
  cmd.Connection = conn
  cmd.CommandType = CommandType.StoredProcedure
  cmd.CommandText = "CNC_UNLOCK_USERS"
  Try
    cmd.ExecuteNonQuery()
  Catch ex As OracleException
    'do something
  End Try
End Using

Based on Executing stored procedure in vb.net (if you later need parameters, please refer to this link). User ID and password are stored in d:\VB\vbtext1.txt, according to your VB6 code. Not sure why it's checking similar location on C:, and never assigns the FileName.

If you need to pass name of stored procedure through command line, you need to switch Startup object to Sub Main (project properties -> Application tab), and then place similar code into a new class:

Public Class Class1
  Shared Sub Main(args() As String)
    'parse args and save it somewhere
    Form1.ShowDialog()
  End Sub
End Class

Before showing a form, make sure you save args(0) as name of your stored procedure to be executed. Or you can have a custom ShowDialog on the form, that takes a parameter of type String, and call that. I would recommend you convert to console application, since it will now show its UI anyway. Then you simply execute above code in Sub Main, changing this line:

cmd.CommandText = "CNC_UNLOCK_USERS"

to this:

cmd.CommandText = args(0)
Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • That is my original solution but they are wanting to execute the stored procedure from a command line parameter. (See the screen shot above). Let say PLSQLSUB.exe is the .NET program then the CNC_UNLOCK_USERS is the stored procedure. The stored procedure cannot be hard coded from the code. Thanks for the response by the way. – Allan Tolentino Jan 12 '13 at 19:53
  • @AllanTolentino: I am confused. You said PLSQLSUB.exe is VB6, not .NET. So converting does not work for you, and you want to run PLSQLSUB from .NET? Please confirm. – Victor Zakharov Jan 12 '13 at 20:21
  • sorry for the confusion. PLSQLSUB.exe is currently VB6 and I'm currently converting into .NET – Allan Tolentino Jan 12 '13 at 20:35
  • @AllanTolentino: I think I got your point now. Please see my edit. – Victor Zakharov Jan 12 '13 at 21:41
  • Sorry but not quite. Example: If i run the compiled .NET program. It should look like this screenshot below: ![EXTERNAL PROGRAM](http://s20.postimage.org/m4n2nbmml/parameterscreen.jpg) – Allan Tolentino Jan 12 '13 at 22:58
  • @AllanTolentino: I don't think you can paste a screenshot into the comment. Use either a link or update your original question. – Victor Zakharov Jan 12 '13 at 23:00
  • I guess that my biggest issue here is that the parameter is in the external program. This external program is a vendor program and not part of the .NET, I need to be able to pass that parameter. Please bear with me. English is my second language. – Allan Tolentino Jan 12 '13 at 23:05
  • @AllanTolentino: I am confused part 2. You want to rewrite the 3rd party software? or the VB6 part? or both? How does it relate to extracting a value of a textbox from this 3rd party software? – Victor Zakharov Jan 12 '13 at 23:06
  • Neolisk, No, i'm trying to read the parameter from 3rd party software. Currently, the PLSQLSUB.exe is vb6 but now i'm trying to convert it into .net console. So once the program run, it should pass the parameter from the 3rd party software. – Allan Tolentino Jan 12 '13 at 23:10
  • @Neolisk It does look like it is using CommandLine arguments. The VB6 Command Method returns everything starting at arg[1] – Mark Hall Jan 12 '13 at 23:20
  • Neolisk, the command line is supposed to be a stored procedure name. If you saw the screenshot, that is the 3rd party software form but the PLSQLSUB.exe is the customize vb invoking the stored procedure from the parameter line. – Allan Tolentino Jan 12 '13 at 23:21
  • 1
    @MarkHall: If 3rd party software calls OP's utility and passes arguments to it through command line - that's one story. If your program starts and steals something from this 3rd party software - that's another one. We need to be firm on who calls what, before proceeding. In other words, sequence of events should be clarified. – Victor Zakharov Jan 12 '13 at 23:23
  • 1. The screen form above is a 3rd party software. 2. The PLSQLSUB.exe is a vb6 code not trying to convert to .NET 3. The parameter text box has the stored procedure name. There is a RUN button within this vendor program. If i click this, the PLSQLSUB.exe should execute and run the stored procedure given in the parameter box. – Allan Tolentino Jan 12 '13 at 23:36
  • @AllanTolentino: 3rd party software is shown as External Program in your original question. It has a RUN button, which currently executes VB6 program (PLSQLSUB.exe). You plan to write a VB.NET program and change 3rd party TextBox'es to run that instead. You would like to know how to read command line arguments in .NET. Is that correct? – Victor Zakharov Jan 12 '13 at 23:41
  • @AllanTolentino: then see the suggestion in my last comment to your question. – Victor Zakharov Jan 13 '13 at 02:16
0

You can use the Environment.GetCommandLineArgs Method to retrive the CommandLine arguments. In further looking at your question you are doing something similar already, the question I have are you trying to import the StoredProcedure or the Name of the StoredProcedure to be run.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • The name, from what I got. – Victor Zakharov Jan 12 '13 at 22:57
  • @Neolisk then your method, or even the one that he showed in his question which is the equivalant of what I posted should work. – Mark Hall Jan 12 '13 at 22:59
  • Mark, that is what i'm trying to use but don't know how to use it i guess. And Yes, I was trying to import the name of stored procedure from the text box of a vendor program. Please help. – Allan Tolentino Jan 12 '13 at 23:01
  • @AllanTolentino You are saying you are trying to import the stored procedure name from the the Textbox of a vendor program, how/what is the commandline that you are using to start your program? or are you trying to read or extract the data from the 3rd party program. – Mark Hall Jan 12 '13 at 23:04
  • @Mark. Yes, i'm trying to read the parameter the 3rd party program, see the screenshot here >> ![EXTERNAL PROGRAM](http://s20.postimage.org/m4n2nbmml/parameterscreen.jpg) – Allan Tolentino Jan 12 '13 at 23:08
  • But the PLSQLSUB.exe is currently a vb6 and I'm trying to convert it to .NET console. – Allan Tolentino Jan 12 '13 at 23:09
  • @AllanTolentino: are you looking for something like [this](http://stackoverflow.com/questions/2551705/capturing-data-from-a-window-in-a-closed-source-third-party-win32-application)? PLSQLSUB does not capture anything from 3rd party program. – Victor Zakharov Jan 12 '13 at 23:09
  • 1
    @AllanTolentino in looking at the VB6 Command Method it states that it returns everything after the program name which is the first CommandLine Argument. So it looks like your program is being called with some CommandLine arguments. Do me a favor and list what `GetCommandLIneArgs` returns. We are kind of flying blind here without being able to see what exactly is happening. – Mark Hall Jan 12 '13 at 23:18
  • Neolisk, it is almost similar except PLSQLSUB.exe (which supposed to be writted in .NET) will be executed within 3rd party program's form. – Allan Tolentino Jan 12 '13 at 23:19
  • @AllanTolentino: I suggest the following - please edit your question and make sure you clearly specify current sequence of events (how it works **at the moment**), and another one - how it is **planned to work** after your changes take place. – Victor Zakharov Jan 12 '13 at 23:25
  • Guys - i apologized for all the confusion. I'll get back to you after I modified the program with all your suggestion and give you updates. Thanks again. Cheers. – Allan Tolentino Jan 13 '13 at 00:21