VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 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
 
————————————————
版权声明:本文为CSDN博主「普通网友」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/eds124/article/details/84187484

相关教程