实例127修改表中的数据

1.在Access2010在创建一个数据库,另存为Login.mdb

2.设计窗体

一个DataGridVeiw,三个Label,一个TrackBar,两个TextBox,两个Button

3.把那个数据库添加数据源,然后写入代码

Imports System.Data.OleDb
Public Class Form1
    Public Function GetConnection() As OleDbConnection
        Return New OleDbConnection(My.Settings.LoginConnectionString)
    End Function
    Private Sub display()
        myConn = GetConnection()
        myConn.Open()

        Dim strSql As String = "Select * from userTable"
        Dim myComm As New OleDbCommand(strSql, myConn)
        Dim myDataReader As OleDbDataReader = myComm.ExecuteReader
        Dim myTable As New DataTable
        myTable.Load(myDataReader)

        myConn.Close()

        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        DataGridView1.DataSource = myTable
        DataGridView1.Rows(0).Cells(0).Selected = True
    End Sub
    Private myConn As OleDbConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        display()
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        txtName.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
        txtPassword.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
        TrackBar1.Value = e.RowIndex + 1
        Label1.Text = "当前记录号" & e.RowIndex + 1
    End Sub

    Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
        txtName.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
        txtPassword.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
    End Sub

    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        txtName.Text = DataGridView1.CurrentRow.Cells(1).Value
        txtPassword.Text = DataGridView1.CurrentRow.Cells(2).Value
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        myConn.Open()

        Dim intID As Integer
        intID = DataGridView1.CurrentCell.RowIndex + 1
        Dim strSql As String = "Update userTable" _
                             & " set userTable.password='" & txtPassword.Text & "'" _
                             & ",userTable.username='" & txtName.Text & "'" _
                             & " Where userTable.id=" & intID.ToString

        Dim myComm As OleDbCommand = New OleDbCommand(strSql, myConn)
        myComm.ExecuteNonQuery()

        myConn.Close()

        display()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ngbshzhn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值