Skip to content

Instantly share code, notes, and snippets.

@messboy
Created January 15, 2014 09:21
Show Gist options
  • Save messboy/8433183 to your computer and use it in GitHub Desktop.
Save messboy/8433183 to your computer and use it in GitHub Desktop.
//聯繫SQL 處理多筆SQL查詢
private void getmutiquery()
{
string cmdtext = "select top 10 * from Orders; select top 11 * from Products";
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand(cmdtext, conn);
SqlDataReader dr = null;
//開啟資料庫連線
conn.Open();
//將指令結果指定給dr
dr = cmd.ExecuteReader();
//dr 讀取SQL第一次查詢資料
while (dr.Read())
{
DropDownList1.Items.Add(dr[1].ToString());
}
//dr 下一筆查詢資料結果
dr.NextResult();
//dr 讀取SQL第二筆查詢資料結果
while (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
//關閉cmd dr 連結資料庫conn的部分,using end會幫我們處理
cmd.Cancel();
dr.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment