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