vb.net使用者管理系統
Imports System.Data.SqlClient
Public Class Form1
Dim connstr As String = "Data Source=.;Initial Catalog=mydata;User ID=sa;Password=sa"
Dim txtusername As TextBox = New TextBox()
Dim txtpassword As TextBox = New TextBox()
WithEvents btnlogin As Button = New Button()
WithEvents dgv As DataGridView = New DataGridView()
Dim tc As TabControl = New TabControl()
Dim tp1 As TabPage = New TabPage("1")
Dim tp2 As TabPage = New TabPage("2")
Dim tp3 As TabPage = New TabPage("3")
Dim tp4 As TabPage = New TabPage("4")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
Me.MaximizeBox = False
Me.AcceptButton = btnlogin
txtpassword.PasswordChar = "*"
btnlogin.Text = "登陸"
txtusername.Location = New Point(100, 100)
txtpassword.Location = New Point(100, 125)
btnlogin.Location = New Point(100, 150)
Me.Controls.Add(txtusername)
Me.Controls.Add(txtpassword)
Me.Controls.Add(btnlogin)
End Sub
Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
Dim username As String = Me.txtusername.Text.Trim()
Dim password As String = Me.txtpassword.Text.Trim()
If False Then
Return
End If
txtusername.Dispose()
txtpassword.Dispose()
btnlogin.Dispose()
Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
Me.MaximizeBox = True
Me.WindowState = FormWindowState.Maximized
dgv.Dock = DockStyle.Fill
tp1.Controls.Add(dgv)
tc.Dock = DockStyle.Fill
tc.Controls.Add(tp1)
tc.Controls.Add(tp2)
tc.Controls.Add(tp3)
tc.Controls.Add(tp4)
Me.Controls.Add(tc)
Using conn As SqlClient.SqlConnection = New SqlClient.SqlConnection()
Using cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
Using sda As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter()
Using dt As DataTable = New DataTable
conn.ConnectionString = connstr
cmd.Connection = conn
cmd.CommandText = "select * from t_user"
sda.SelectCommand = cmd
sda.Fill(dt)
dgv.DataSource = dt
dgv.EditMode = DataGridViewEditMode.EditOnEnter
dgv.Columns(0).ReadOnly = True
End Using
End Using
End Using
End Using
End Sub
Private Sub dgv_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellValueChanged
Using conn As SqlClient.SqlConnection = New SqlClient.SqlConnection()
Using cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand()
Dim username As String = dgv.Rows(e.RowIndex).Cells(1).Value.ToString()
Dim gender As String = dgv.Rows(e.RowIndex).Cells(2).Value.ToString()
If Not gender.Equals("m") And Not gender.Equals("f") Then
MsgBox("性別必須是m或者f")
Return
End If
Dim birthday As String = dgv.Rows(e.RowIndex).Cells(3).Value.ToString()
If Not IsDate(birthday) Then
MsgBox("生日必須是日期型別")
Return
End If
Dim homeaddress As String = dgv.Rows(e.RowIndex).Cells(4).Value.ToString()
Dim idnumber As String = dgv.Rows(e.RowIndex).Cells(5).Value.ToString()
Dim id As String = dgv.Rows(e.RowIndex).Cells(0).Value.ToString()
conn.ConnectionString = connstr
cmd.Connection = conn
cmd.CommandText = "update t_user set username = @username, gender = @gender, birthday = @birthday, homeaddress = @homeaddress, idnumber = @idnumber where id = @id"
cmd.Parameters.AddWithValue("username", username)
cmd.Parameters.AddWithValue("gender", gender)
cmd.Parameters.AddWithValue("birthday", birthday)
cmd.Parameters.AddWithValue("homeaddress", homeaddress)
cmd.Parameters.AddWithValue("idnumber", idnumber)
cmd.Parameters.AddWithValue("id", id)
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Class