您好我一直停留很長一段時間在這個簡單而鈍消息「必須聲明標量變量」VB.net SQL錯誤必須聲明標量變量
我有插入vb.net和SQL代碼數據轉換成簡單的表格。它適用於示例數據,但是當我嘗試使用參數(insertcommand.Parameters.Add(「@ ID」,SqlDbType.Int,3).Value = 50)時,它給我帶來了錯誤。如果我只用一個數字替換@ID就行。
最終.Value = txtid.text如果我可以得到參數的工作。
感謝您的幫助。
Dim connection As New SqlConnection
Dim connectionString As String =
"Data Source=LAPTOP-PC\SQLEXPRESS2008;Initial Catalog=golf;" &
"Integrated Security=True"
connection.ConnectionString = connectionString
Dim insertcommand As New SqlCommand
insertcommand.Connection = connection
insertcommand.Parameters.Add("@ID", SqlDbType.Int, 3).Value = 50
'Must declare the scalar variable
Dim insertStatement As String =
"INSERT INTO Golf (ID, Title, Firstname, Surname, Gender, DOB, Street, Suburb, City, [Available week days], Handicap)" &
"Values(@ID, 'Mr', 'Howard', 'The Duck', 'm', '12/12/23', 'asd', 'sdf', 'City', '0', '56') "
Using insertconnection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter()
Try
insertconnection.Open()
adapter.InsertCommand = insertconnection.CreateCommand
adapter.InsertCommand.CommandText = insertStatement
adapter.InsertCommand.ExecuteNonQuery()
MsgBox("Data Inserted !! ")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
它的工作!非常感謝! – netchicken 2012-03-28 17:04:28