添加,修改,删除

前面声明

Imports System.Data

Imports System.Data.OleDb

――――――――――――――――

Private ds As DataSet = New DataSet

Private ds1 As DataSet = New DataSet

Private sqlStr As String

Private strcon As String = "Provider=SQLOLEDB.1;Data Source=(local);

        Initial Catalog=数据库名;User ID=sa;Password=sa"

Public DBcon As New OleDbConnection(strcon)

――――――――――――――――

Private Sub DBView()

DBcon.Open() sqlStr = "select * from 表"

Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

ds.Clear()

adt.Fill(ds)

Me.DataGrid1.DataSource = ds.Tables(0)

DBcon.Close()

End Sub

――――――――――――――――

 

添加:

Try

   Dim comid, comname, danwei, comtype As String

 

   comid = Trim(Me.TextBox1.Text)

   comname = Me.TextBox2.Text

   danwei = Trim(Me.ComboBox1.SelectedItem)

   comtype = Trim(Me.ComboBox2.SelectedItem)

   DBcon.Open()

   If comname = "" Then

       MsgBox("comname can't be Empty!", MsgBoxStyle.Critical, Me.Text)

       Return

   End If

   If danwei = "" Then

       MsgBox("danwei can't be Empty!", MsgBoxStyle.Critical, Me.Text)

       Return

   End If

   If comtype = "" Then

       MsgBox("comtype can't be Empty!", MsgBoxStyle.Critical, Me.Text)

       Return

   End If

 

   if Me.CheckBox1.Checked = False Then

        Dim sqlStr As String = "insert into TMMCom values ('" & Trim(comid) & "','" & comname & "','" & danwei & "','" & comtype & "',0)"

        Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

        ds.Clear()

        adt.Fill(ds)

        DBcon.Close()

        DBView()

   Else

        Dim sqlStr As String = "insert into TMMCom values ('" & Trim(comid) & "','" & comname & "','" & danwei & "','" & comtype & "',1)"

        Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

        ds.Clear()

        adt.Fill(ds)

        DBcon.Close()

        DBView()

    End If

 Catch ex As Exception

     MsgBox("comid can't be Repeat!", MsgBoxStyle.Critical, Me.Text)

End Try

 

修改:

Dim comid, comname, danwei, comtype As String

comid = Trim(Me.TextBox1.Text)

    comname = Me.TextBox2.Text

    danwei = Trim(Me.ComboBox1.SelectedItem)

    comtype = Trim(Me.ComboBox2.SelectedItem)

If comname = "" Then

    MsgBox("comname can't be Empty!", MsgBoxStyle.Critical, Me.Text)

Return

End if

If danwei = "" Then

    MsgBox("danwei can't be Empty!", MsgBoxStyle.Critical, Me.Text)

    Return

End if

If comtype = "" Then

    MsgBox("comtype can't be Empty!", MsgBoxStyle.Critical, Me.Text)

    Return

End if

 

DBcon.Open()

    Dim sqlStr As String = "update TMMCom set cComName='" & comname & "',cDanwei='" & danwei & "',cComType='" & comtype & "'where iComID ='" & comid & "'"

   Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

    ds.Clear()

    adt.Fill(ds)

    DBcon.Close()

    DBView()

 

删除:

Dim comid As Int32

comid = CInt(Me.TextBox1.Text)

 

DBcon.Open()

Dim sqlStr As String = "update TMMCom set cComName='',cDanwei='',cComType=''where iComID =" & comid & ""

Dim adt As New OleDbDataAdapter(sqlStr, DBcon)

ds.Clear()

adt.Fill(ds)

DBcon.Close()

DBView()

原文地址:https://www.cnblogs.com/sishierfei/p/1610382.html