vb.net 操作数据库

======在窗体中添加DataGrid==========
 
Sub classquery()
        Dim str As String
        Dim ds As New DataSet
        Dim da As SqlDataAdapter

        str = "Data Source=localhost;Initial Catalog = Student;integrated Security=true"
        Dim con As New SqlConnection(str)
        con.Open()
        Dim sql As String = "select * from student_Info where 教室代码 = '" & TextBox1.Text.ToString().Trim() & "' "
        da = New SqlDataAdapter(sql, con)
        da.Fill(ds)
        DataGrid1.DataSource = ds.Tables(0)
    End Sub
======此方法是检验数据是否已存在============
 Sub singleYanzheng()
        Dim str As String
        str = "Data Source=localhost;Initial Catalog = Student;integrated Security=true"
        Dim con As New SqlConnection(str)
        con.Open()
        Dim sql As String = "select * from depart_Info where 系部ID='" & TextBox1.Text.ToString().Trim() & "' "
        Dim cmd As New SqlCommand(sql, con)
        Dim reader As SqlDataReader
        reader = cmd.ExecuteReader
        If reader.Read() = True Then
            MsgBox("该部门号已存在")
        Else
            addDepartInfo()
            MsgBox("success")
            clear()
        End If
    End Sub
========执行插入动作===========
 Sub addDepartInfo()
        Dim str As String
        str = "Data Source=localhost;Initial Catalog = Student;integrated Security=true"
        Dim con As New SqlConnection(str)
        con.Open()
        Dim sql As String = "insert into depart_Info(系部ID,系部名称,系主任) values('" & TextBox1.Text.ToString().Trim() & "','" & TextBox2.Text.ToString().Trim() & " ','" & TextBox3.Text.ToString().Trim() & "') "
        Dim cmd As New SqlCommand(sql, con)
        Try
            cmd.ExecuteNonQuery()  '执行SQL语句
        Catch e As Exception
            Console.WriteLine(e.Message) '无法执行时提示出错信息
        End Try
        Console.WriteLine("Record Added")
    End Sub

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值