Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Browsing datareader results
Message
 
To
18/08/2008 06:25:48
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01339462
Message ID:
01340673
Views:
12
-----
As you know, Data readers are objects that implement the System.Data.IDataReader interface. A
data reader is a fast, unbuffered, forward-only, read-only connected stream that retrieves
data on a per-row basis. It reads one row at a time as it loops through a result set.

You can’t directly instantiate a data reader; instead, you create one with the
ExecuteReader method of a command.

To print out the data from a DataReader you need to loop through:
while (myDataReader.Read())
{
   // print one row at a time
   Console.WriteLine("{0}", myDataReader[0]);
}
To bind data to a DataGridView we need a DataTable.
As an example to show your data in a DataGridView,
one possible way is to code as following:
...
...
string sqlCnStr = @"Data Source=.\mySQLserver;Initial Catalog=Northwind;Integrated Security=True";
using (SqlConnection northWindSqlCn = new SqlConnection(sqlCnStr))
{
	try
	{
            string nwSqlStr = "select productID, ProductName, UnitPrice from Products order by UnitPrice";
	    SqlCommand nwSqlCmd = new SqlCommand(nwSqlStr, northWindSqlCn);
	    SqlDataAdapter nwDataAdptr = new SqlDataAdapter(nwSqlCmd);
	    DataSet nwDataSet = new DataSet();
	    nwDataAdptr.Fill(nwDataSet, "myTable");
	    dataGridView1.DataSource = nwDataSet.Tables["myTable"];
	}
	catch (Exception ex)
	{
	    MessageBox.Show(ex.Message.ToString());
	}
	finally
	{
	    if (northWindSqlCn != null)
		northWindSqlCn.Close();
	}
}
...
...
To bing data to your DataGridView see the following page in MSDN:

How to: Bind Data to the Windows Forms DataGridView Control
http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

Hope it helps
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform