0

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

例子。这是第3方程序的截图:

外部项目

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
4

2 回答 2

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)
于 2013-01-12T19:46:14.477 回答
0

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

于 2013-01-12T21:51:47.607 回答