我有一個GridView
。我正在嘗試使用DataTable
將數據庫表格數據顯示到網格中。因此,我將查詢結果保存在DataTable
中,但數據未顯示。這是我的代碼。請幫忙。在c#數據表中顯示數據庫表數據,但數據表爲空
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dataTable = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["gridconnection"].ConnectionString;
string query = "select * from GridExcel";
SqlConnection con1 = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query, con1);
con1.Open();
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
// this will query your database and return the result to your
datatable
da.Fill(dataTable);
Gridview1.DataSource = dataTable;
Gridview1.DataBind();
ViewState["CurrentTable"] = dataTable;
con1.Close();
}
}
con.Open();或con1.Open()? –
從GridExcel中選擇*是否返回任何內容? –
'SqlDataAdapter'不需要打開連接 –