//vb实现数据库的连接,修改,删除,插入(ADO.Net) - 云代码 http://yuncode.net/code/c_508403a91ab6b23
Dim conn As New SqlConnection
Dim rs As New SqlDataAdapter
Public Function connect_db() As SqlConnection
constr = "Server=PC-201105311336\SQLEXPRESS;uid=sa;pwd=123456;database=jiaxiaoai"
'rs.Fill(dataset, "student")
conn = New SqlConnection(constr)
Private Sub 全部学生信息_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load'窗口初始化函数
sql = "select * from student"
rs = New SqlDataAdapter(sql, conn)
DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据
DataGridView1.Columns(0).HeaderText = "学号" '设置列名
DataGridView1.Columns(1).HeaderText = "姓名"
DataGridView1.Columns(2).HeaderText = "年龄"
DataGridView1.Columns(3).HeaderText = "成绩"
rownum.Text = Str(DataGridView1.RowCount) '获取行数
xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值
nametext.Text = DataGridView1.Rows(0).Cells(1).Value
If (DataGridView1.Rows(0).Cells(2).Value = 0) Then
agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value)
If (DataGridView1.Rows(0).Cells(3).Value = 0) Then
scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value)
Private Sub selectbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesselectbtn.Click
sql = "select * from student where name like 'l%'"
rs = New SqlDataAdapter(sql, conn)
DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据
DataGridView1.Columns(0).HeaderText = "学号" '设置列名
DataGridView1.Columns(1).HeaderText = "姓名"
DataGridView1.Columns(2).HeaderText = "年龄"
DataGridView1.Columns(3).HeaderText = "成绩"
rownum.Text = Str(DataGridView1.RowCount) '获取行数
xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值
nametext.Text = DataGridView1.Rows(0).Cells(1).Value
If (DataGridView1.Rows(0).Cells(2).Value = 0) Then
agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value)
If (DataGridView1.Rows(0).Cells(3).Value = 0) Then
scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value)
Private Sub DataGridView1_CellMouseClick(ByVal sender As System.Object, ByVal e AsSystem.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick '鼠标点击单元格触发事件
'DataGridView1.CurrentRow.Index获取当前单元格的行号
xuehaotext.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value '获取datagridview中的某个值
nametext.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value
If (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = 0) Then
agetext.Text = Str(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value)
If (DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = 0) Then
scoretext.Text = Str(DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value)
Private Sub updatebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesupdatebtn.Click
If agetext.Text = "" Then
sql = "update student set sid='" & xuehaotext.Text & "', name='" & nametext.Text & "',age='" & age & "',score='" & scoretext.Text & "' where sid='" & DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value & "'"
dc = New SqlCommand(sql, conn)
row = dc.ExecuteNonQuery()
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value = xuehaotext.Text
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value = nametext.Text
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value = agetext.Text
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value = scoretext.Text
Private Sub insertbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesinsertbtn.Click
If agetext.Text = "" Then
sql = "insert into student values('" & xuehaotext.Text & "', '" & nametext.Text & "','" & age &"','" & scoretext.Text & "')"
dc = New SqlCommand(sql, conn)
row = dc.ExecuteNonQuery()
sql = "select * from student"
rs = New SqlDataAdapter(sql, conn)
DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据
DataGridView1.Columns(0).HeaderText = "学号" '设置列名
DataGridView1.Columns(1).HeaderText = "姓名"
DataGridView1.Columns(2).HeaderText = "年龄"
DataGridView1.Columns(3).HeaderText = "成绩"
rownum.Text = Str(DataGridView1.RowCount) '获取行数
xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值
nametext.Text = DataGridView1.Rows(0).Cells(1).Value
If (DataGridView1.Rows(0).Cells(2).Value = 0) Then
agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value)
If (DataGridView1.Rows(0).Cells(3).Value = 0) Then
scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value)
Private Sub deletebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handlesdeletebtn.Click
If agetext.Text = "" Then
sql = "delete student where sid='" & DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value & "'and name='" & DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value & "'"
dc = New SqlCommand(sql, conn)
row = dc.ExecuteNonQuery()
sql = "select * from student"
rs = New SqlDataAdapter(sql, conn)
DataGridView1.DataSource = dt '将表中的数据显示到datagridView控件中,绑定数据
DataGridView1.Columns(0).HeaderText = "学号" '设置列名
DataGridView1.Columns(1).HeaderText = "姓名"
DataGridView1.Columns(2).HeaderText = "年龄"
DataGridView1.Columns(3).HeaderText = "成绩"
rownum.Text = Str(DataGridView1.RowCount) '获取行数
xuehaotext.Text = DataGridView1.Rows(0).Cells(0).Value '获取datagridview中的某个值
nametext.Text = DataGridView1.Rows(0).Cells(1).Value
If (DataGridView1.Rows(0).Cells(2).Value = 0) Then
agetext.Text = Str(DataGridView1.Rows(0).Cells(2).Value)
If (DataGridView1.Rows(0).Cells(3).Value = 0) Then
scoretext.Text = Str(DataGridView1.Rows(0).Cells(3).Value)
转载于:https://blog.51cto.com/laoye/1105590