System.Data.SqlClient Namespace SqlCommandBuilder Class
Initializes a new instance of the SqlCommandBuilder class with the associated SqlDataAdapter object.
[ VB ]
Public Sub New ( _
ByVal adapter As SqlDataAdapter _
)
[ C# ]
public SqlCommandBuilder (
SqlDataAdapter adapter
);
[ C++ ]
public: SqlCommandBuilder (
SqlDataAdapter* adapter
);
[ JScript ]
public function SqlCommandBuilder (
adapter : SqlDataAdapter
);
- adapter
- The name of the SqlDataAdapter.
The SqlCommandBuilder registers itself as a listener for RowUpdating events that are generated by the SqlDataAdapter specified in this property.
When you create a new instance SqlCommandBuilder, any existing SqlCommandBuilder associated with this SqlDataAdapter is released.
The following example uses the SqlCommand, along with SqlDataAdapter and SqlConnection, to select rows from a data source. The example is passed an initialized DataSet, a connection string, a query string that is a Transact-SQL SELECT statement, and a string that is the name of the database table. The example then initializes an SqlCommandBuilder.
public DataSet SelectSqlSrvRows ( DataSet myDataSet, string myConn,
string query, string myTableName ) {
SqlConnection myConn = new SqlConnection ( myConn );
SqlDataAdapter myDataAdapter = new SqlDataAdapter ( );
myDataAdapter.SelectCommand = new SqlCommand ( query, myConn );
SqlCommandBuilder custCB = new SqlCommandBuilder ( myDataAdapter );
myConn.Open ( );
DataSet myDataSet = new DataSet ( );
myDataAdapter.Fill ( myDataSet, "Customers" );
// code to modify data in dataset here
// without the SqlCommandBuilder this line would fail
myDataAdapter.Update ( myDataSet, "Customers" );
myConn.Close ( );
}
Public Function SelectSqlSrvRows ( myDataSet As DataSet, myConn As String, _
query As String, myTableName As String ) As DataSet
Dim myConn As New SqlConnection ( myConn )
Dim myDataAdapter As New SqlDataAdapter ( )
myDataAdapter.SelectCommand = New SqlCommand ( query, myConn )
Dim custCB As SqlCommandBuilder = New SqlCommandBuilder ( myDataAdapter )
myConn.Open ( )
Dim myDataSet As DataSet = New DataSet
myDataAdapter.Fill ( myDataSet, "Customers" )
' code to modify data in DataSet here
' without the SqlCommandBuilder this line would fail.
myDataAdapter.Update ( myDataSet, "Customers" )
myConn.Close ( )
End Function |
|
C# |
VB |
SqlCommandBuilder Members SqlCommandBuilder Constructor Overload List