vb.net 应用程序中的 System.Data.dll 中发生类型为“System.Data.SqlClient.SqlException”的第一次机会异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13340890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll in vb.net application
提问by Javed Salat
I am creating a vb.net application in which I'm getting error "
A first chance exception of type 'System.Data.SqlClient.SqlException'occurred in System.Data.dll"I dont know how to solve this error and there are 4 fields in tables
我正在创建一个 vb.net 应用程序,在该应用程序中出现错误“类型的第一次机会异常'System.Data.SqlClient.SqlException'发生在System.Data.dll"我不知道如何解决此错误并且表中有 4 个字段
- o_id primary key auto increment by 1
c_idstatic- owner_name
varchar() investment
Dim connetionString As String Dim cnn As SqlConnection Dim cmd As New SqlCommand connetionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\javed\Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\Database1.mdf';Integrated Security=True;User Instance=True" cnn = New SqlConnection(connetionString) Try cnn.Open() MsgBox("Connection is Open ! ") cmd.Connection = cnn cmd.CommandText = "INSERT INTO owner_detail(c_id, o_name, investment) values(@cid,'" + TextBox1.Text + "','" + TextBox2.Text + "')" cmd.ExecuteNonQuery() MsgBox("Data is Successfully Inserted! ") cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try
- o_id 主键自增 1
c_id静止的- 所有者姓名
varchar() 投资
Dim connetionString As String Dim cnn As SqlConnection Dim cmd As New SqlCommand connetionString = "Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\javed\Documents\Visual Studio 2008\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\Database1.mdf';Integrated Security=True;User Instance=True" cnn = New SqlConnection(connetionString) Try cnn.Open() MsgBox("Connection is Open ! ") cmd.Connection = cnn cmd.CommandText = "INSERT INTO owner_detail(c_id, o_name, investment) values(@cid,'" + TextBox1.Text + "','" + TextBox2.Text + "')" cmd.ExecuteNonQuery() MsgBox("Data is Successfully Inserted! ") cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try
回答by WozzeC
You are not adding the parameter @cid anywhere.
您没有在任何地方添加参数 @cid。
cmd.Parameters.Add(new SqlParameter("@cid", SqlDbType.Int))
cmd.Parameters("@cid").Value = Idvalue

