【问题标题】:Converting VBA function to VB.net to get sql data将 VBA 函数转换为 VB.net 以获取 sql 数据
【发布时间】:2019-08-25 05:14:33
【问题描述】:

我正在尝试将 VBA 代码转换为 VB.net,我已经做到了,但我无法将结果集转换为 vb.net。 RS 在 VBA 中是“暗淡的结果集”,我认为我可以将其更改为数据集,但在“.fields”和其他选项中出现错误?

 Function GetG(sDB As String, sServ As String, sJob As String) As String

        'sDB = Database name, sServ = Server\Instance, path = job.path
        Dim conString As String = ("driver={SQL Server};server = " & 
         TextBox1.Text & " ; uid = username;pwd=password:database = " & 
         TextBox2.Text)
        Dim RS As DataSet
        Dim conn As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand


        conn.Open()

'This is where my problems are occuring

        cmd = New SqlCommand("SELECT [ID],[Name] FROM dbo.PropertyTypes")

           Do While Not RS.Tables(0).Rows.Count = 0
            If RS.Fields(1).Value = sJob Then
                GetG = RS.Fields(0).Value
                GetG = Mid(GetG, 2, 36)
                Exit Do
            End If
            DataSet.MoveNext

        Loop
        conn.Close
    End Function

【问题讨论】:

  • SqlConnectionSqlCommand 实现 IDisposable 并应包裹在 Using 块中; conn.Close 不需要拼写出来,处理连接(退出 Using 范围)会解决这个问题。
  • 您还可以在 SQL 语句中创建 WHERE 子句,以仅提取具有 sJob 值的行。这将大大减少您的代码,甚至可能不需要 do while 循环,具体取决于您的表中是否有多个具有相同 sJob 值的行。

标签: sql .net vba vb.net basic


【解决方案1】:

您似乎没有填充您的数据集。因此,当您尝试循环访问它时,它未初始化或为空。

查看此答案以查看示例:Get Dataset from DataBase

【讨论】:

    【解决方案2】:

    根据我的理解和一些猜测,这就是我想出的我认为你想要的东西。

    正如我在上面的评论中所说,您似乎可以使用 WHERE 子句来获取您想要的确切记录(假设 sJob 的单个实例出现在 name 列中)。

    根据输入参数构建连接字符串,而不是表单上的控件。毕竟,这就是为什么您允许传递参数的原因。另请注意,有一个可能感兴趣的 SqlCommandBuilder 对象。但是现在

    Function GetG(sDB As String, sServ As String, sJob As String) As String
        'we'll pretend your connectionstring is correct based off of the sDB and sServ arguments
        Dim conStr As String = ("driver={SQL Server};server = " & sServ & " ; uid = username;pwd=password:database = " & sDB)
        'Create a connection and pass it your conStr
        Using con As New SqlConnection(conStr)
            con.Open() 'open the connection
            'create your sql statement and add the WHERE clause with a parameter for the input argument 'sJob'
            Dim sql As String = "SELECT [ID], [Name] FROM dbo.PropertyTypes WHERE [Name] = @job"
            'create the sqlCommand (cmd) and pass it your sql statement and connection
            Using cmd As New SqlCommand(sql, con)
                'add a parameter so the command knows what @job holds
                cmd.Parameters.Add(New SqlParameter("@job", SqlDbType.VarChar)).Value = sJob
                'Now that have the command built, we can pass it to a reader object 
                Using rdr As SqlDataReader = cmd.ExecuteReader
                    rdr.Read()
                    'i admin i'm a little confused here on what you are
                    'trying to achieve so ID may not be what you are
                    'really wanting to get a substring of.
                    Return rdr("ID").ToString.Substring(2, 36)
                End Using
            End Using
        End Using
    End Function
    

    看看这是否有效的一个例子可能是调用一个消息框来显示结果。对于此示例,我将假设 TextBox3 拥有您想要的 sJob。有了这些知识,您就可以这样做:

    MessageBox.Show(GetG(TextBox2.Text, TextBox1.Text, TextBox3.Text))
    

    这应该会在消息框中产生结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      相关资源
      最近更新 更多