ADO Performance
home
DataReader
Reader DT+Load
Next Result
ExecuteScalar
Dataset X Scalar
Table Mapping
DATAREADER
Categories:
Beverages
Condiments
Confections
Dairy Products
Grains/Cereals
Meat/Poultry
Produce
Seafood
Products:
ProductID
ProductName
UnitPrice
UnitsInStock
1
notebook
1500.0000
17
2
Coca-Cola 111
4.0000
4
24
Guaraná Fantástica
4.5000
20
34
Sasquatch
14.0000
111
35
Steeleye Stout
18.0000
20
38
Côte de Blaye
263.5000
17
39
Chartreuse verte
18.0000
69
43
Ipoh Coffee
46.0000
17
67
Laughing Lumberjack Lager
14.0000
52
70
Outback Lager
100.0000
15
75
Rhönbräu Klosterbier
7.7500
125
76
Lakkalikööri
18.0000
57
Show code / exibir código
using System.Data.SqlClient;
using System.Web.Configuration;
string conStr = WebConfigurationManager.ConnectionStrings["ConnStringKey"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string sql = "Select CategoryID, CategoryName FROM Categories ORDER BY CategoryName";
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
dropCategories.DataSource = reader;
dropCategories.DataValueField = "CategoryID";
dropCategories.DataTextField = "CategoryName";
dropCategories.DataBind();
reader.Close();
dropCategories_SelectedIndexChanged(sender, e);
}
finally
{
conn.Close();
}
}
}
protected void dropCategories_SelectedIndexChanged(object sender, EventArgs e)
{
string sql = "Select ProductID, ProductName, UnitPrice, UnitsInStock FROM Products Where CategoryID=@cat";
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter p1 = new SqlParameter("@cat", SqlDbType.Int);
p1.Value = dropCategories.SelectedItem.Value;
cmd.Parameters.Add(p1);
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
griddata.DataSource = reader;
griddata.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}