在ado.net中更新查询

时间:2011-01-15 15:48:24

标签: vb.net 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()

1 个答案:

答案 0 :(得分:1)

您不需要在SQL字符串中引用该参数。

尝试以下方法:

sqlComm.CommandText = "update committe_member set name = @name where name = 'member1'"

我还要在将参数值添加到参数集合之前设置参数值

paramString.Value = TextBox1.Text
sqlComm.Parameters.Add(paramString)