数据库开发021 DataAdapter对象的Update方法

      当调用Update方法时,DataAdapter将检查参数DataSet每一行的RowState属性,根据RowState属性来检查DataSet里的每一行是否改变及改变的类型,并依次执行所需的INSERT、UPDATE或DELETE语句,将改变提交到数据库中,同时返回影响DataSet
的行数。

数据库开发021 DataAdapter对象的Update方法_第1张图片

Imports System.Data.SqlClient
Public Class Form1
    Dim conn As SqlConnection
    Dim da As SqlDataAdapter
    Dim ds As New DataSet           '必须加“NEW”关键字
    Dim dt As New DataTable
    Private Function GetConnection() As SqlConnection
        Return New SqlConnection(My.Settings.SalesConnectionString)
    End Function

    Private Sub DisplayData1(ByVal str As String) '用DataReader对象
        DataGridView1.DataSource = Nothing

        conn = GetConnection()
        conn.Open()
        Dim comm As New SqlCommand(str, conn)
        Dim dr As SqlDataReader = comm.ExecuteReader
        Dim dt As New DataTable
        dt.Load(dr)
        conn.Close()

        DataGridView1.DataSource = dt
    End Sub

    Private Sub DisplayData2(ByVal str As String) '用DataSet对象和Adapter的Fill方法
        DataGridView1.DataSource = Nothing

        conn = GetConnection()
        conn.Open()
        da = New SqlDataAdapter(str, conn)
        da.Fill(ds, "dt")

        conn.Close()

        DataGridView1.DataSource = ds.Tables("dt")
    End Sub

    Private Sub DataReaderData(ByVal str As String)
        conn = GetConnection()
        conn.Open()

        Dim comm As New SqlCommand(str, conn)
        Dim dr As SqlDataReader
        dr = comm.ExecuteReader
        Dim strOutput As String = ""
        Try
            While dr.Read
                strOutput += dr("学号").ToString.PadLeft(10) + Space(5)
                strOutput += dr("姓名").ToString.PadLeft(10) + Space(5)
                strOutput += dr("语文").ToString.PadLeft(10) + Space(5)
                strOutput += dr("数学").ToString.PadLeft(10) + Space(5)
                strOutput += dr("英语").ToString.PadLeft(10) + Space(5)
                strOutput += vbCrLf
            End While
        Catch ex As Exception
            MsgBox("出现异常")
        Finally
            dr.Close()
            conn.Close()
        End Try
        'Label2.Text = strOutput
    End Sub

    Private Sub UpdateData()
        conn = GetConnection()
        conn.Open()
        Dim comm As New SqlCommand
        Dim trans As SqlTransaction
        trans = conn.BeginTransaction
        comm.Connection = conn
        comm.Transaction = trans
        Try
            comm.CommandText = "Update grade set 数学=95 Where 姓名 like '%周%'"
            comm.ExecuteNonQuery()
            comm.CommandText = "Update grade set 数学=65 Where 姓名 like '%张%'"
            comm.ExecuteNonQuery()

            trans.Commit()
            Label1.Text = "事务运行成功"
        Catch ex As Exception
            trans.Rollback()
            Label1.Text = "有错"
        Finally
            conn.Close()
        End Try
    End Sub

    Private Sub InsertRecord()
        'conn = GetConnection()
        'conn.Open()
        'Dim strSql As String = "Insert into grade(学号,姓名,语文,数学,英语) " & _
        '"Values('" & txtId.Text & "','" & txtName.Text & "','" & _
        'txtChinese.Text & "','" & txtMaths.Text & "','" & txtEnglish.Text & "')"
        'Dim comm As New SqlCommand(strSql, conn)
        'comm.ExecuteNonQuery()
        'conn.Close()

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        conn = GetConnection()
        conn.Open()
        da = New SqlDataAdapter("Select * from grade", conn)
        da.Fill(ds, "mytbl")

        Dim myDataRow As DataRow
        myDataRow = ds.Tables("mytbl").NewRow
        myDataRow("学号") = txtId.Text
        myDataRow("姓名") = txtName.Text
        myDataRow("语文") = txtChinese.Text
        myDataRow("数学") = txtMaths.Text
        myDataRow("英语") = txtEnglish.Text
        ds.Tables("mytbl").Rows.Add(myDataRow)

        Dim scb As New SqlCommandBuilder(da)
        da.Update(ds, "mytbl")

        DisplayData1("Select * from grade")
        conn.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.AutoSizeColumnsMode = ColumnHeaderAutoResizeStyle.ColumnContent
        DisplayData1("Select * from grade")
    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        conn = GetConnection()
        conn.Open()
        da = New SqlDataAdapter("Select * from grade", conn)
        da.Fill(ds, "mytbl")

        Dim myDataRow As DataRow
        ds.Tables("mytbl").PrimaryKey = New DataColumn() {ds.Tables("mytbl").Columns("学号")}
        myDataRow = ds.Tables("mytbl").Rows.Find(txtId.Text)
        myDataRow("学号") = txtId.Text
        myDataRow("姓名") = txtName.Text
        myDataRow("语文") = txtChinese.Text
        myDataRow("数学") = txtMaths.Text
        myDataRow("英语") = txtEnglish.Text

        Dim scb As New SqlCommandBuilder(da)
        da.Update(ds, "mytbl")
        conn.Close()

        DisplayData1("Select * from grade")
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        txtId.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value
        txtName.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
        txtChinese.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
        txtMaths.Text = DataGridView1.Rows(e.RowIndex).Cells(3).Value
        txtEnglish.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value
    End Sub
End Class

 

你可能感兴趣的:(#,VB2010数据库开发入门)