VB.NET SQL查询失败。必须声明Scalar但已经声明

时间:2018-07-10 22:13:31

标签: sql sql-server vb.net

我已经很长时间没有使用VB.NET了,上一次我用VB编码时是VB6。天哪,我想念过去的美好时光。无论如何,尝试在数据库上运行查询。我的代码如下:

    'Create a Connection object.
    myConn = New SqlConnection("Server=localhost\SQLEXPRESS;Database=dwt-monitor;Trusted_Connection=True;")

    'Create a Command object.

    myCmd = myConn.CreateCommand
    myCmd.CommandText = "select log_time as [Log Time], log_name as [Log Name], log_desc as [Log Description], type_name as [Log Type] from log as log
    left join log_type as lot on log.log_id=lot.type_id where type_name= @variable"
    myConn.Open()
    myCmd.Parameters.AddWithValue("@variable", type)
    myCmd.ExecuteNonQuery()

    Dim dataAdapter = New SqlDataAdapter(myCmd.CommandText, myConn.ConnectionString)
    Dim table As New DataTable()


    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    dataAdapter.Fill(table)

    dgvHardwareLog.DataSource = table
    myConn.Close()

我收到此错误:

  

System.Data.SqlClient.SqlException:'必须声明标量变量   “ @variable”

谁能告诉我我要去哪里错了? Iam添加值。但是,a,有些不正确。

谢谢:)

4 个答案:

答案 0 :(得分:2)

尝试更改以下行:

Dim dataAdapter = New SqlDataAdapter(myCmd.CommandText, myConn.ConnectionString)

以下,传递了命令,而不仅仅是命令文本。此外,删除 myConn.ConnectionString

Dim dataAdapter = New SqlDataAdapter(myCmd)

此外,您可以删除以下行:

myCmd.ExecuteNonQuery()

问题是您在 SqlDataAdapter 构造函数中传递了命令文本,并且由于添加了 @variable 参数而使其丢失到 myCmd


https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataadapter-parameters上找到更多详细信息。

答案 1 :(得分:0)

我将再次检查SSMS中的SQL语句,以查看它是否有效并返回您期望的结果。我将SQL语句和连接传递给Command的构造函数。

尽管我离开了.AddWithValue(),但实际上它应该是.Add方法,其中包括字段的数据类型。

使用DataAdapter.Fill()时,无需打开连接。如果连接已关闭,它将打开并自动关闭,但是如果在调用.Fill()时打开了连接,则它将保持打开状态。

您正在执行两次查询。一旦使用myCmd.ExecuteNonQuery(它一个查询,一个Select语句,Insert,Update,Delete是非查询)。然后再次使用.Fill()

Using...End Using语句可确保具有.Dispose()方法的对象被正确关闭和处置,即使发生错误。

Private Sub OPCode()
        Dim table As New DataTable()
        Dim strSQL As String = "select log_time as [Log Time], log_name as [Log Name], log_desc as [Log Description], type_name as [Log Type] from log as log
    left join log_type as lot on log.log_id=lot.type_id where type_name= @variable"
        Using myConn As New SqlConnection("Server=localhost\SQLEXPRESS;Database=dwt-monitor;Trusted_Connection=True;")
            Using myCmd As New SqlCommand(strSQL, myConn)
                myCmd.Parameters.AddWithValue("@variable", Type)
                Using dataAdapter As New SqlDataAdapter(myCmd)
                    table.Locale = System.Globalization.CultureInfo.InvariantCulture
                    dataAdapter.Fill(table)
                End Using
            End Using
        End Using
        dgvHardwareLog.DataSource = table
    End Sub

答案 2 :(得分:0)

myCmd.CommandText = "declare @variable as varchar(100) 
select log_time as [Log Time], log_name as [Log Name], log_desc as [Log Description], type_name as [Log Type] from log as log
left join log_type as lot on log.log_id=lot.type_id where type_name= @variable"

在使用变量时,必须声明一个变量,而没有声明,这就是错误。声明变量将解决错误

答案 3 :(得分:0)

您正在做同一件事的多种形式。 CommandDataAdapter都运行查询,因此您只需要一个查询,这取决于要对检索到的数据进行什么操作。

我将按以下方式修复您的代码:

myConn = New SqlConnection("Server=localhost\SQLEXPRESS;Database=dwt-monitor;Trusted_Connection=True;")

Dim qry as String = "select log_time as [Log Time], log_name as [Log Name], log_desc as [Log Description], type_name as [Log Type] from log as log
left join log_type as lot on log.log_id=lot.type_id where type_name= @variable"

Dim dataAdapter = New SqlDataAdapter(qry, myConn)
dataAdapter.SelectCommand.Parameters.AddWithValue("@variable", {your variable here})

'I assume Type is not an actual variable you have as I would think it would confuse the compiler from the keyword with the same name.
Dim table As New DataTable()

table.Locale = System.Globalization.CultureInfo.InvariantCulture
dataAdapter.Fill(table)
dataAdapter.Dispose()

dgvHardwareLog.DataSource = table

我也更喜欢使用Using语句,但又不想更改太多代码以至于无法识别。