vb.net连接sqlserver2012执行一条删除语句
接触了两天VB.net,头很痛,与其继续头疼还不如求教网友。
问题如下,公司需要一个桌面可执行程序,往数据库执行一调DELETE语句,只需要执行这个语句就可以了,麻烦各位有爱的网友帮下忙,本人感激不尽。
[解决办法]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn1.Open()
//删除前先判断是否和数据库匹配、可根据你的需求不进行此操作
Dim str1 As String = "select * from jiben where 商品编号='" & TextBox1.Text & "'"
Dim comm1 As New OleDb.OleDbCommand(str1, conn1)
Dim adpt1 As New OleDb.OleDbDataAdapter(comm1)
Dim ds As New DataSet
Dim ret As MsgBoxResult
ret = MessageBox.Show("确定删除商品信息?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
conn1.Close()
conn1.Open()
If ret = MsgBoxResult.Ok Then
Try
Dim str2 As String = "delete from jiben where 商品编号=" & TextBox1.Text & ""
Dim comm2 As New OleDb.OleDbCommand(str2, conn1)
comm2.ExecuteNonQuery()
MessageBox.Show("商品删除成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("删除失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
MessageBox.Show(ex.Message)
End Try
End If
conn1.Close()
conn1.Open()
ds.Clear()
adpt1.Fill(ds, "jiben")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "jiben"
conn1.Close()
End Sub
Imports System.Data.SqlClient
Public Shared ConnectionString As String = "Data Source=" + Common.Server + ";Initial Catalog=" + Common.Db + ";User ID=" + Common.UId + ";Password=" + Common.Pwd
''' <summary>
''' 执行SQL语句,返回影响的记录数
''' </summary>
''' <param name="SQLString">SQL语句</param>
''' <returns>影响的记录数</returns>
Public Shared Function ExecuteSql(ByVal SQLString As String) As Integer
Using connection As New SqlConnection(ConnectionString)
Using cmd As New SqlCommand(SQLString, connection)
Try
connection.Open()
Dim rows As Integer = cmd.ExecuteNonQuery()
Return rows
Catch e As System.Data.SqlClient.SqlException
connection.Close()
Throw e
End Try
End Using
End Using
End Function