Привет, я давно застрял в этом простом, но тупом сообщении "Должен объявить скалярную переменную"
У меня есть код 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