0
在以下asp c#代碼中,我試圖使用SqlDataReader顯示數據庫表的內容,但它不工作。此代碼僅處理按鈕單擊事件。我怎樣才能使它工作?爲什麼這段代碼沒有在瀏覽器中顯示錶格?
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.DataSource = null;
GridView1.DataBind();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (con)
{
con.ConnectionString = CS;
DataTable dTbl = new DataTable();
dTbl.Columns.Add("ID");
dTbl.Columns.Add("Name");
dTbl.Columns.Add("Salary");
SqlCommand vmd = new SqlCommand();
vmd.Connection = con;
vmd.CommandText = "select * from [dbo].[employee]";
con.Open();
SqlDataReader dr = vmd.ExecuteReader();
while (dr.Read())
{
DataRow dRow = dTbl.NewRow();
dRow["ID"] = dr[0];
dRow["Name"] = dr[1];
dRow["Salary"] = dr[2];
dTbl.Rows.Add(dRow);
}
}
}