你能告诉我这段代码有什么问题吗?
我需要用来DataAdapter
插入表格吗?
我知道connectionString
没问题,因为我在服务器资源管理器上对其进行了测试。
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
上面的代码在之后就失败了ExecuteNonQuery()
,无法弄清楚为什么。
目标字段(SQL Server 2008):
AREA varchar(100) NOT NULL ,
USER varchar(100) NOT NULL
我收到的例外是:Connection property has not initialized