• 添加,修改,删除


    前面声明

    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()

  • 相关阅读:
    没有被实例化的类 中的 非static成员函数竟然也可以被调用。。。前提是该成员函数没有用到成员变量
    c++注意
    关于类大小的小试验
    C语言|博客作业02
    在C#中进行时间和时间戳的转换
    正则表达式中匹配中括号 [ ]
    在C#中将对象序列化成Json格式
    在MSSQL中的简单数据类型递归
    HTML中padding和margin的区别和用法
    C#中的对称加密
  • 原文地址:https://www.cnblogs.com/sishierfei/p/1610382.html
Copyright © 2020-2023  润新知