Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用sql server更新VB.net的按钮代码_Sql Server_Vb.net - Fatal编程技术网

Sql server 使用sql server更新VB.net的按钮代码

Sql server 使用sql server更新VB.net的按钮代码,sql-server,vb.net,Sql Server,Vb.net,我创建了一个图书馆管理系统。在这里,如果我想更新一本书的特定记录,它将更新SQL server数据库中的所有记录。如何编写只更新特定记录的代码。这是我的密码 Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;int

我创建了一个图书馆管理系统。在这里,如果我想更新一本书的特定记录,它将更新SQL server数据库中的所有记录。如何编写只更新特定记录的代码。这是我的密码

Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click

    con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
    con.Open()
    Dim comd As New SqlCommand("update  Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "'", con)
    comd.ExecuteNonQuery()
    MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

您需要在SqlCommand中添加WHERE子句,以便SQLServer知道要更新的记录。如果没有WHERE子句,它将更新整个表。见下文:

 con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
    con.Open()
    Dim comd As New SqlCommand("update  Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "' WHERE Book_Id='{**Put your book id here**}'", con)
    comd.ExecuteNonQuery()
    MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

您需要在SqlCommand中添加WHERE子句,以便SQLServer知道要更新的记录。如果没有WHERE子句,它将更新整个表。见下文:

 con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
    con.Open()
    Dim comd As New SqlCommand("update  Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "' WHERE Book_Id='{**Put your book id here**}'", con)
    comd.ExecuteNonQuery()
    MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

在SQL命令中添加一个
WHERE
子句,以指定将更新哪本书。
使用要更新的图书的ID号。
要避免在sql命令中串联,请使用参数
@

Dim comd As New SqlCommand("update  Book set Book_Id=@bookID, Bk_Name=@bkName, Author_Name=@author, Year_of_release=@release, Availability_of_bks=@avail WHERE Book_Id=@whereID", con)
comd.Parameters.Add("@bookID", SqlDbType.String).Value = TextBox1.Text
comd.Parameters.Add("@bkName", SqlDbType.String).Value = TextBox2.Text
comd.Parameters.Add("@author", SqlDbType.String).Value = TextBox3.Text
comd.Parameters.Add("@release", SqlDbType.String).Value = TextBox4.Text
comd.Parameters.Add("@avail", SqlDbType.String).Value = TextBox5.Text
comd.Parameters.Add("@whereID", SqlDbType.String).Value = "Book ID HERE"
comd.ExecuteNonQuery()
MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

在SQL命令中添加一个
WHERE
子句,以指定将更新哪本书。
使用要更新的图书的ID号。
要避免在sql命令中串联,请使用参数
@

Dim comd As New SqlCommand("update  Book set Book_Id=@bookID, Bk_Name=@bkName, Author_Name=@author, Year_of_release=@release, Availability_of_bks=@avail WHERE Book_Id=@whereID", con)
comd.Parameters.Add("@bookID", SqlDbType.String).Value = TextBox1.Text
comd.Parameters.Add("@bkName", SqlDbType.String).Value = TextBox2.Text
comd.Parameters.Add("@author", SqlDbType.String).Value = TextBox3.Text
comd.Parameters.Add("@release", SqlDbType.String).Value = TextBox4.Text
comd.Parameters.Add("@avail", SqlDbType.String).Value = TextBox5.Text
comd.Parameters.Add("@whereID", SqlDbType.String).Value = "Book ID HERE"
comd.ExecuteNonQuery()
MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

您需要在SqlCommand中添加where原因。您需要在SqlCommand中添加where原因。如果Book_Id是一个数字,如10,则不需要引号,因此查询将变成where Book_Id=10。明白了,感谢您的回答,非常欢迎!如果答案对您有帮助,请将其标记为答案。如果Book_Id是一个数字,如10,则您不需要引号,因此查询将变为Book_Id=10。明白了,谢谢您的回答。非常欢迎!如果答案对您有帮助,请将其标记为答案。