无法理解SQL命令中的@参数

时间:2018-07-23 20:39:48

标签: vb.net

早上好

我无法理解vb.net中的@参数。如果我们以msdn的示例代码为例:

    ' Update the demographics for a store, which is stored 
    ' in an xml column.
    Dim commandText As String = _
     "UPDATE Sales.Store SET Demographics = @demographics " _
     & "WHERE CustomerID = @ID;"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(commandText, connection)

        ' Add CustomerID parameter for WHERE clause.
        command.Parameters.Add("@ID", SqlDbType.Int)
        command.Parameters("@ID").Value = customerID

        ' Use AddWithValue to assign Demographics.
        ' SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml)

似乎根本没有问题,但是当您尝试向混合中添加更多参数时,它似乎没有任何作用。添加多个参数的正确语法是什么?

这是我的代码:

    query &= "INSERT INTO "
    query &= imageProcAlgo.GetSqlTable()
    query &= " ( @dateTimeStampCol , @numberOfBlobsCol , @AvgLengthCol , @avgWidthCol ) "
    query &= "Values (@dtsvalue, @nbOfBlobsValue, @avgLengthValue, @avgWidthValue)"

,在这里我尝试向这些参数添加值:

Using conn As New SqlConnection(connectStr)

    Using comm As New SqlCommand(query)
        With comm
            .Connection = conn
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@dateTimeStampCol", "DateTimeStamp")
            .Parameters.AddWithValue("@numberOfBlobsCol", "NumberOfBlob")
            .Parameters.AddWithValue("@AvgLengthCol", "AvgWidth")
            .Parameters.AddWithValue("@avgWidthCol", "AvgLength")
            .Parameters.AddWithValue("@dtsvalue", "CURRENT_TIMESTAMP")
            .Parameters.AddWithValue("@nbOfBlobsValue", imageProcAlgo.GetnumberOfBlobs().ToString())
            .Parameters.AddWithValue("@avgLengthValue", imageProcAlgo.GetAvgWidth())
            .Parameters.AddWithValue("@avgWidthValue", imageProcAlgo.GetAvgLength())


        End With
        Try
            conn.Open()
            comm.ExecuteNonQuery()
            conn.Close()
        Catch ex As SqlException
            MessageBox.Show(ex.Message.ToString(), "Error Message")
        End Try
    End Using

当我尝试执行这段代码时,我收到一条错误消息,指出@datetimestampcol等不是有效的列名。

我似乎不明白我的代码与MSDN之间有什么区别,但是我感觉非常接近。任何让我更了解的指针都可以吗?找出我哪里出了问题?

谢谢

0 个答案:

没有答案