Search This Blog

Showing posts with label ADO. Show all posts
Showing posts with label ADO. Show all posts

Sunday, May 6, 2007

ADO.NET Data Binding Examples

An example shows how you can create a DataTableReader and bind it to a DataGridView

using (SqlConnection cn = new SqlConnection(cnStr)) {
SqlCommand cmd = new SqlCommand(sqlAllCustomers, cn); SqlDataAdapter adpt = new
SqlDataAdapter(cmd); DataTable dtCustomers = new DataTable("Customers");
adpt.Fill(dtCustomers); DataTableReader dtRdr = ds.CreateDataReader();
dgvCustomers.DataSource = dtRdr;
}


Load method of a DataSet or a DataTable, you can pass in a DataTableReader or any reader class that implements the IDataReader interface.

DataTableReader dtRdr = dt1.CreateDataReader();
DataTable dt2 = new DataTable();
dt2.Load(dtRdr);


DataSet can be filled from the Products table of the Northwind database.
string sSQL = "SELECT * FROM Products";
string sConnString = "Server=(local);Database=Northwind;Integrated Security=SSPI;";
SqlDataAdapter oDa = new SqlDataAdapter();
DataSet oDs = new DataSet();
using(SqlConnection oCn = new SqlConnection(sConnString))
{
SqlCommand oSelCmd = new SqlCommand(sSQL, oCn);
oSelCmd.CommandType = CommandType.Text;
oDa.SelectCommand = oSelCmd; oDa.Fill(oDs, "Products");
}