ExecuteNonQuery () dla Insert
Czy możesz mi powiedzieć, co jest nie tak z tym kodem?
Czy muszę korzystaćDataAdapter
wstawić do stołu?
Wiem, żeconnectionString
jest ok, ponieważ przetestowałem go w Eksploratorze serwerów.
Dim mydao As New Connection
Dim connectionString As String = mydao.GetConnectionString()
Dim connection As New SqlConnection(connectionString)
Dim cmd As New SqlCommand
Public Function add(ByVal area As String, ByVal user As String) As Integer
cmd.CommandText = "INSERT into Area (Area, user) VALUES ('" + area + "','" + user + "')"
Try
connection.Open()
Dim cant As Integer = cmd.ExecuteNonQuery()'it throws exception here
connection.Close()
Return cant
Catch ex As Exception
Console.WriteLine(ex.Message)
Return 0
End Try
End Function
Powyższy kod zawodzi zaraz poExecuteNonQuery()
i nie potrafię zrozumieć, dlaczego.
TARGET FIELDS (SQL Server 2008):
AREA varchar(100) NOT NULL ,
USER varchar(100) NOT NULL
Wyjątkiem, który otrzymuję, jest:Connection property has not initialized