SqlCommandBuilder 可以幫我們節省很多撰寫 SelectCommand、InsertCommand、UpdateCommand、DeleteCommand 這幾個 SQL 陳述式的屬性,在開發上很方便,但效能上會有所差別。

Imports System.Data.SqlClient
Public Class Form1

Using connection As New SqlConnection(connectionString)
Dim talbe As New DataTable
Dim adapter As New SqlDataAdapter()

Dim sqlstr As String = _
                  "select * from product where (company like '模式')"

設定 SqlDataAdapter 的 SelectCommand 屬性,則可以建立 SqlCommandBuilder 物件來為單一資料表更新自動產生 Transact-SQL 陳述式。然後,任何未設定的其他 Transact-SQL (INSERT、UPDATE 或 DELETE) 陳述式會由SqlCommandBuilder 產生。

Private Sub Form1_Load(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles MyBase.Load

adapter.SelectCommand = New SqlCommand(sqlstr, connection)
conn.Open()
adapter.Fill(talbe)
DataGridView1.DataSource = Table
conn.Close()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

Dim cb As New SqlCommandBuilder(adapter)
Dim count As Integer = da.Update(talbe)

MessageBox.Show(count)


End Class

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 肯尼&那那 的頭像
    肯尼&那那

    肯尼那那

    肯尼&那那 發表在 痞客邦 留言(0) 人氣()