[問題] show database in table or gridview

作者: ya12590 (juke)   2014-09-04 23:25:25
大家好
小弟第一次在此發文 有些問題想請教一下 謝謝
像我是想要按一個button後 便可以載入此資料表的資料 show在gridview上
以下是我的程式碼
protected void Button3_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand() ;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM D";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
conn.Close();
}
我參考過很多資料了
我也覺得這樣應該按下button後 可以show出我資料表的所有資料在gridview裡面
但按下button後完全沒有反應
請問 我是不是哪裡出了問題
我的環境是VS2010 我是用SQL server management studio 來建立我的資料庫以及資料表
謝謝 如果有其他東西我沒附上的 麻煩告知我一下 謝謝
希望可以早日解決 卡很久了><
作者: O187 (187cm)   2014-09-04 23:31:00
你確定datatable內有資料嗎?
作者: ya12590 (juke)   2014-09-04 23:32:00
有的!! 我確定
作者: GoalBased (Artificail Intelligence)   2014-09-05 07:50:00
沒bind 阿GridView1.DataSource = dt;後面一行GridView1.DataBind()
作者: jcpf126 (jcpf126)   2014-09-05 09:07:00
推樓上,另外建議你研究SqlDataAdapter,可以寫得更簡潔
作者: ya12590 (juke)   2014-09-05 10:31:00
謝謝各位馬上試

Links booklink

Contact Us: admin [ a t ] ucptt.com