Retrieve data source by using the DataReader
Example with while loop
while loopvar nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT ProductID, UnitPrice FROM Products";
connection.Open();
DbDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
MessageBox.Show(rdr["ProductID"] + ": " + rdr["UnitPrice"]);
}
connection.Close();Example with DataTable
DataTablevar nw = ConfigurationManager.ConnectionStrings["nw"];
var connection = new SqlConnection();
connection.ConnectionString = nw.ConnectionString;
var cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT ProductID, ProductName FROM Products";
connection.Open();
var rdr = cmd.ExecuteReader();
var products = new DataTable();
products.Load(rdr, LoadOption.Upsert);
connection.Close();
cmbProducts.DataSource = products;
cmbProducts.DisplayMember = "ProductName";
cmbProducts.ValueMember = "ProductID";PreviousBuild command objects and query data from data sourcesNextManage data by using the DataAdapter and TableAdapter
Last updated