从VB.NET中的第三方程序调度程序解析命令行

时间:2013-01-12 14:05:54

标签: vb.net vb.net-2010 vb6-migration

我在创建一个在Oracle数据库中执行存储过程的控制台vb.net程序时遇到问题。如果存储过程将在代码中进行硬编码,我可以这样做。问题是,存储过程需要放在供应商程序的参数中。目前,我们使用VB6程序来执行此操作。现在,他们想将这个vb6程序转换为.NET,但我不知道该怎么做。我尝试了下面的方法,但它没有从第三方程序传递命令参数。

实施例。这是第三方程序的屏幕截图:

EXTERNAL PROGRAM http://s20.postimage.org/m4n2nbmml/parameterscreen.jpg

PLSQLSUB.exe程序是一个调用存储过程CNC_UNLOCK_USERS的vb6。

这是VB6代码,目前正在工作,但我不知道如何在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

一位朋友告诉我使用下面的代码,但我无法传递存储过程。任何帮助将不胜感激。

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

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你想将现有的VB6转换为VB.NET,即从VB.NET运行存储过程。由于您的SP没有参数,请使用以下代码:

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

基于Executing stored procedure in vb.net(如果您以后需要参数,请参阅此链接)。根据您的VB6代码,用户ID和密码存储在d:\VB\vbtext1.txt中。不知道为什么它会检查C:上的类似位置,并且永远不会分配FileName

如果需要通过命令行传递存储过程的名称,则需要将Startup object切换到Sub Main(项目属性 - >应用程序选项卡),然后将类似的代码放入新类中:

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

在显示表单之前,请确保将args(0)保存为要执行的存储过程的名称。或者,您可以在表单上拥有自定义ShowDialog,其中包含String类型的参数,然后调用它。我建议您转换为控制台应用程序,因为它现在将显示其UI。然后,您只需在Sub Main中执行上面的代码,更改此行:

cmd.CommandText = "CNC_UNLOCK_USERS"

到此:

cmd.CommandText = args(0)

答案 1 :(得分:0)

您可以使用Environment.GetCommandLineArgs方法来检索CommandLine参数。在进一步查看你的问题时,你已经做了类似的事情,我的问题是你试图导入StoredProcedure或要运行的StoredProcedure的名称。