Generating SqlCommand Objects with the SqlCeCommandBuilder


Generating SqlCommand Objects with the SqlCeCommandBuilder

Looking back at Listing 7.11, you can see that several lines of code are required to set up the update, insert, and delete commands of the SqlCeDataAdapter . The SqlCeCommandBuilder can do this work for you, but the SqlCeDataAdapter must be handling a single source table.

First, you need to initialize the SqlCeDataAdapter and its SelectCommand property. You then create SqlCeCommandBuilder that passes the SqlCeDataAdapter as a parameter to the SqlCeCommandBuilder constructor. The SqlCeCommandBuilder will then create a command for SqlCeDataAdapter 's UpdateCommand , InsertCommand , and DeleteCommand properties. Listing 7.14 demonstrates how to use the SqlCeCommandBuilder to build a SqlCeDataAdapter for the Package table.

Listing 7.14 Using the SqlCeCommandBuilder
 C# public static SqlCeDataAdapter GetPackageDataAdapter(SqlCeConnection conn){   string dmlPackageInfo = "SELECT * FROM Package";   SqlCeDataAdapter daPackages = new SqlCeDataAdapter();   daPackages.SelectCommand = new SqlCeCommand(dmlPackageInfo, conn);   SqlCeCommandBuilder cmdBldr = new SqlCeCommandBuilder(daPackages);   MessageBox.Show(cmdBldr.GetUpdateCommand().CommandText);   MessageBox.Show(cmdBldr.GetInsertCommand().CommandText);   MessageBox.Show(cmdBldr.GetDeleteCommand().CommandText);   return daPackages; } VB Function _ GetPackageDataAdapter(ByVal conn As SqlCeConnection) _ As SqlCeDataAdapter   Dim dmlPackageInfo As String   Dim daPackages As SqlCeDataAdapter   dmlPackageInfo = "SELECT * FROM Package"   daPackages = New SqlCeDataAdapter   daPackages.SelectCommand = New SqlCeCommand(dmlPackageInfo, conn)   Dim cmdBldr As SqlCeCommandBuilder   cmdBldr = New SqlCeCommandBuilder(daPackages)   MessageBox.Show(cmdBldr.GetUpdateCommand().CommandText)   MessageBox.Show(cmdBldr.GetInsertCommand().CommandText)   MessageBox.Show(cmdBldr.GetDeleteCommand().CommandText)   Return daPackages End Function 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net