更新 ado.net 中的查询
我想更新表中的一列,我已经编写了代码,它运行良好,没有任何错误,它还显示确认对话框,但表未更新,代码有什么问题。
Dim sqlConn As New SqlClient.SqlConnection
sqlConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\housingsociety.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Try
sqlConn.Open()
Catch sqlError As Exception
MsgBox(sqlError.Message, 0, "Connection Error!")
End Try
Dim sqlComm As New SqlClient.SqlCommand
sqlComm.Connection = sqlConn
sqlComm.CommandText = "update committe_member set name = '@name' where name = 'member1'"
Dim paramString As New SqlClient.SqlParameter("@name", SqlDbType.VarChar, 50)
paramString.Direction = ParameterDirection.Input
sqlComm.Parameters.Add(paramString)
paramString.Value = TextBox1.Text
sqlComm.ExecuteNonQuery()
MsgBox("Record Sucessfully Altered", 0, "Confirmation!")
sqlConn.Close()
I wanted to update a column in my table, i have written the code it runs fine without any error also it displays the confirmation dialog box but the table is not updated whats wrong with the code.
Dim sqlConn As New SqlClient.SqlConnection
sqlConn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\housingsociety.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
Try
sqlConn.Open()
Catch sqlError As Exception
MsgBox(sqlError.Message, 0, "Connection Error!")
End Try
Dim sqlComm As New SqlClient.SqlCommand
sqlComm.Connection = sqlConn
sqlComm.CommandText = "update committe_member set name = '@name' where name = 'member1'"
Dim paramString As New SqlClient.SqlParameter("@name", SqlDbType.VarChar, 50)
paramString.Direction = ParameterDirection.Input
sqlComm.Parameters.Add(paramString)
paramString.Value = TextBox1.Text
sqlComm.ExecuteNonQuery()
MsgBox("Record Sucessfully Altered", 0, "Confirmation!")
sqlConn.Close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要在 SQL 字符串中引用该参数。
请尝试以下操作:
在将参数值添加到参数集合之前,我还会设置参数值:
You do not need to quote the parameter in your SQL string.
Try the following:
I would also set the parameter value before adding it to the parameters collection: