删除SQL语句

时间:2014-08-12 14:58:41

标签: vb.net ms-access

我有一个更新的sql语句,可以通过vb.net在msaccess中更新记录。反正有没有把它变成一个删除声明?所以我有一个按钮来更新记录,如果我想删除记录。

更新代码:

Dim sqlupdate As String
' Here we use the UPDATE Statement to update the information. To be sure we are   
' updating the right record we also use the WHERE clause to be sureno information  
' is added or changed in the other records  
'sqlupdate = "UPDATE Table1 SET Title=@Title, YearofFilm=@YearofFilm, Description=@Description, Field1=@Field1 WHERE ID='" & TextBox5.Text & "'"
'WHERE YearofFilm='" & TextBox2.Text & "'"
'sqlupdate = "UPDATE Table1 SET Title=@Title, YearofFilm=@YearofFilm, Description=@Description, " & "Field1=@Field1 WHERE ID='" & TextBox5.Text & "'"
sqlupdate = "UPDATE Table1 SET Title=@Title, YearofFilm=@YearofFilm, " & _
            "Description=@Description, Field1=@Field1 WHERE ID=@id"

Dim cmd As New OleDbCommand(sqlupdate, con1)
' This assigns the values for our columns in the DataBase.   
' To ensure the correct values are written to the correct column  
cmd.Parameters.AddWithValue("@Title", TextBox1.Text)
cmd.Parameters.AddWithValue("@YearofFilm", Convert.ToInt32(TextBox2.Text))
cmd.Parameters.AddWithValue("@Description", TextBox3.Text)
cmd.Parameters.AddWithValue("@Field1", TextBox4.Text)
cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(TextBox5.Text))

' This is what actually writes our changes to the DataBase.  
' You have to open the connection, execute the commands and  
' then close connection.  
con1.Open()
cmd.ExecuteNonQuery()
con1.Close()

' This are subs in Module1, to clear all the TextBoxes on the form  
' and refresh the DataGridView on the MainForm to show our new records.  
ClearTextBox(Me)
Me.Close()
RefreshDGV()

1 个答案:

答案 0 :(得分:2)

应该是这样的

Dim ConnString As String = "yourConnectionString"

Dim SqlString As String = "Delete From Table1 Where ID=@id"

Using conn As New OleDbConnection(ConnString)

  Using cmd As New OleDbCommand(SqlString, conn)

   cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(TextBox5.Text))

   conn.Open()

   cmd.ExecuteNonQuery()

conn.Close()

  End Using

End Using

问候!