SQL查询未在VB.NET中运行

时间:2016-03-24 13:41:02

标签: sql sql-server vb.net

我对VB.NET更新,我试图在VB.NET中执行SQL查询,但没有在输出中显示任何值。你可以帮我找一下我错的地方。

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"

    Dim objConn As New SqlConnection(sConnectionString)
    objConn.Open()

    Dim sw ,readerObj
    Dim sSQL As String = "select top 1  " & sw & " = e.import  from tblrelcoms r , [beant].[dbo].tblequipments e where r.IDEquipment = e.IDEquipment"
    Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
    Dim objCmd As New SqlCommand(sSQL, objConn)
    objCmd.ExecuteNonQuery()

    TextBox1.Text = sw.ToString()

1 个答案:

答案 0 :(得分:2)

您遇到的问题是,您不能将变量连接到SQL并期望在SQL执行后更新它。

ExecuteScalar可能是实现目标的最简单方法:

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"

Dim sSQL as string = "SELECT TOP 1 e.import  " _
                    "FROM   tblrelcoms r " & _
                    "       INNER JOIN [beant].[dbo].tblequipments e " & _
                    "           ON r.IDEquipment = e.IDEquipment " & _
                    "ORDER BY e.Import;"

Using connection = new SqlConnection(sConnectionString)
    Using command = New SqlCommand(sSQL, connection)

        connection.Open()
        TextBox1.Text = command.ExecuteScalar().ToString()

    End Using
End Using

虽然如果您需要多个列,那么您可以使用数据阅读器:

Dim sConnectionString As String _
    = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"

Dim sSQL as string = "SELECT TOP 1 e.import  " _
                    "FROM   tblrelcoms r " & _
                    "       INNER JOIN [beant].[dbo].tblequipments e " & _
                    "           ON r.IDEquipment = e.IDEquipment " & _
                    "ORDER BY e.Import;"

Using connection = new SqlConnection(sConnectionString)
    Using command = New SqlCommand(sSQL, connection)

        connection.Open()
        Using reader = command.ExecuteReader()

            If reader.Read()
                TextBox1.Text = reader.GetString(0)
            End If
        End Using

    End Using
End Using

我也做了其他一些改变。

  1. 添加了Using块以确保IDisposable个对象被正确处置。
  2. 将ANSI 89隐式连接的sql连接语法更新为ANSI 92显式连接,因为名称表明您使用的语法已过时24年。开始使用新语法的原因有很多,本文很好地介绍了这一点:Bad habits to kick : using old-style JOINs
  3. 在您的sql中添加了ORDER BY子句。没有订单的TOP 1会给你不确定的结果(除非你只有一条记录,在这种情况下,前1名是多余的)
  4. 更复杂的解决方案是使用输出参数,这些参数会起作用,看起来更符合您最初想要达到的效果,但对于这种情况(在我看来)是过度的:

    Dim sConnectionString As String _
        = "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
    
    Dim sSQL as string = "SELECT TOP 1 @Output = e.import  " _
                        "FROM   tblrelcoms r " & _
                        "       INNER JOIN [beant].[dbo].tblequipments e " & _
                        "           ON r.IDEquipment = e.IDEquipment " & _
                        "ORDER BY e.Import;"
    
    Using connection = new SqlConnection(sConnectionString)
        Using command = New SqlCommand(sSQL, connection)
    
            connection.Open()
            Dim p As SqlParameter = command.Parameters.Add("@Output", SqlDbType.VarChar, 255)
            p.Direction = ParameterDirection.InputOutput
    
            command.ExecuteNonQuery();
    
            TextBox1.Text = p.Value.ToString()
    
        End Using
    End Using
    

    *请原谅任何语法错误,我多年没用过VB.Net了,有些c#quirks可能在下面,比如我不记得你是不是要用括号对于无参数的方法,或者如果你不能...希望有足够的基本结构来帮助你入门