Use SqlCommandBuilder in asp.net

Source: Internet
Author: User
asp.net use SqlCommandBuilder, use it to generate ADD, delete, select and other SQL statements, also more convenient, the following gives an example

Private Sub Usecommandbuilder ()
Dim connectionString as String = _
"Integrated SECURITY=SSPI; Persist security Info=false; "+ _
"Initial catalog=northwind;data source=lap800"
Dim Connection As SqlConnection = _
New SqlConnection (ConnectionString)
Connection. Open ()
Try
Dim command as SqlCommand = _
New SqlCommand ("SELECT * from Customers", connection)

Dim Adapter As SqlDataAdapter = _
New SqlDataAdapter (Command)
Dim Builder As SqlCommandBuilder = _
New SqlCommandBuilder (Adapter)
' Show commands
Console.WriteLine (builder. GetInsertCommand (). CommandText)
Console.WriteLine (builder. GetUpdateCommand (). CommandText)
Console.WriteLine (builder. GetDeleteCommand (). CommandText)

Dim table as DataTable = New DataTable
Adapter. Fill (table)

' Use Insert
Dim row as DataRow = table. NewRow
Row ("CustomerID") = "Paulk"
Row ("CompanyName") = "Pauly ' s edibles"
Row ("ContactName") = "Paul Kimmel"
Row ("ContactTitle") = "The Fat man"
Table. Rows.Add (Row)

' Use Update
Dim results () as DataRow = _
Table. Select ("CustomerID = ' ALFKI '")
Results (0) ("Region") = "DE"

' Use Delete
Dim Deletethese () as DataRow = _
Table. Select ("CustomerID = ' Farla '")
For each row in Deletethese
Row. Delete ()
Next

Adapter. Update (table)
Table. AcceptChanges ()

Catch ex as Exception
Debug.WriteLine (ex. Message)
Throw
Finally
Connection. Close ()
End Try
End Sub

End Module



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.