实例136SqlServer更新数据

通过SMSS创建Login.mdf数据库并录入测试数据,接着在VS2010中建立VB项目,将Login.mdf作为数据源并设计窗体,最后编写代码实现数据库更新操作。

1.打开SMSS,创建一个Login.mdf数据库

录入测试数据

2.VS2010,创建一个VB项目,添加数据源Login.mdf,并保存

3.设计窗体

4.代码

Imports System.Data.SqlClient
Public Class Form1
    Public Function GetConnection() As SqlConnection
        Return New SqlConnection(My.Settings.LoginConnectionString)
    End Function
    Dim conn As SqlConnection = GetConnection()

    Private Sub Display1()
        conn.Open()

        Dim strSql As String = "Select * from userTable"
        Dim comm As New SqlCommand(strSql, conn)
        Dim myReader As SqlDataReader = comm.ExecuteReader
        Dim myTable As New DataTable
        myTable.Load(myReader)
        DataGridView1.DataSource = myTable

        conn.Close()

        DataGridView1.Rows(0).Cells(0).Selected = True
        txtUsername.Text = DataGridView1.CurrentRow.Cells(1).Value
        txtPassword.Text = DataGridView1.CurrentRow.Cells(2).Value
    End Sub
    Private Sub Display2()
        conn.Open()

        Dim strSql As String = "Select * from userTable"
        Dim comm As New SqlCommand(strSql, conn)
        Dim myReader As SqlDataReader = comm.ExecuteReader
        Dim myTable As New DataTable
        myTable.Load(myReader)
        DataGridView2.DataSource = myTable

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

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        txtUsername.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
        txtPassword.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
    End Sub

    Private Sub DataGridView1_RowHeaderCellChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.RowHeaderCellChanged
        txtUsername.Text = DataGridView1.Rows(e.Row.Index).Cells(1).Value
        txtPassword.Text = DataGridView1.Rows(e.Row.Index).Cells(2).Value
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Display1()
        txtUsername.Text = ""
        txtPassword.Text = ""

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If txtUsername.Text = "" Or txtPassword.Text = "" Then
            MsgBox("输入用户名和密码")
            txtUsername.Focus()
            Exit Sub
        End If

        conn.Open()
        Dim strSql As String = "Insert into usertable (username,password) " _
                               & "values ('" & txtUsername.Text & "','" & txtPassword.Text & "')"
        Dim comm As New SqlCommand(strSql, conn)
        comm.ExecuteNonQuery()
        conn.Close()

        Display2()

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim intID As Integer = InputBox("输入要删除的序号")

        conn.Open()
        Dim strSql As String = "Delete userTable Where id=" & intID.ToString
        Dim comm As New SqlCommand(strSql, conn)
        comm.ExecuteNonQuery()
        conn.Close()

        Display2()
    End Sub

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

        Dim intID As Integer = DataGridView1.SelectedRows(0).Cells(0).Value

        conn.Open()
        Dim strSql As String = "Update userTable" _
                               & " set username='" & txtUsername.Text & "',password='" & txtPassword.Text & "'" _
                               & " where id=" & intID
        Dim comm As New SqlCommand(strSql, conn)
        comm.ExecuteNonQuery()
        conn.Close()

        Display2()
    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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ngbshzhn

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

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

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

打赏作者

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

抵扣说明:

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

余额充值