Pages

Men

rh

4/13/2013

Example of SQL Command Builder

The following example uses the SqlCommand, along SqlDataAdapter and SqlConnection, to select rows from a data source. The example is passed 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 creates a SqlCommandBuilder.

public static DataSet SelectSqlRows(string connectionString, string queryString, string tableName)
 {
 using (SqlConnection connection = new SqlConnection(connectionString))
 {
     SqlDataAdapter adapter = new SqlDataAdapter();
     adapter.SelectCommand = new SqlCommand(queryString, connection);
     SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
     connection.Open();
     DataSet dataSet = new DataSet();
     adapter.Fill(dataSet, tableName);
      //code to modify data in DataSet here builder.GetUpdateCommand();
      //Without the SqlCommandBuilder this line would fail
     adapter.Update(dataSet, tableName);
     return dataSet;
  }
}
source collected from Microsift MSDN

No comments :

Post a Comment