Sunday, June 7, 2009

SQL query in DataSet Or DataTable

More
--------------------------------

Here's a simple C# example:

DataSet ds = new DataSet();
//populate from commercial database or read offline XML file

string sql = "SELECT TRIM(name), COUNT(name) As 'Total' FROM table1 GROUP BY name ORDER BY 1";
DataView dv = QueryADataSet.DsCommand.Execute(sql, ds);
GridView1.DataSource = dv;
GridView1.DataBind();

Native assemblies for .NET 1.1 and .NET 2.0/3.0.

--------------------------------

//LINQ Example
DataRow row = customers.AsEnumerable()
.Where(i => i.Field("CustID") == 4)
.FirstOrDefault();

//LINQ - Strongly Typed DataSet
DataRow row = ds.Customers.AsEnumerable()
.Where(i => i.CustID == 4)
.FirstOrDefault();

//DataTable Example
DataRow row = customers.Select("CustID = 4")[0];

//DataView Example
DataView dv = new DataView(customers);
dv.RowFilter = "CustID = 4";

--------------------------------

DataView dv = new DataView(MyDataset.Tables["TableName"], "Column1 = " + TestNumtextBox.Text + "and Column2 Like '%" + VehNumtextBox.Text+ "%'","", DataViewRowState.CurrentRows);

you can iterate through the rows in dataview like this

foreach (DataRowView drv in dv)

{

value = (int)drv["column1"];

}

--------------------------------

No comments:

Post a Comment

Popular Posts