驗證 ASP.NET Web 網頁中的使用者輸入 Virtual Floppy Drive(虛擬軟碟機)
六月 22

其實 WinForm 跟 WebForm 在連結資料庫方面其實差不了太多。

C#:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     //第一:連結SQL資料庫
  4.     string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\test_Data.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
  5.     SqlConnection conn = new SqlConnection(connstr);
  6.     conn.Open();
  7.  
  8.     //第二:執行SQL指令
  9.     string sqlstr = "select * from test";
  10.     SqlCommand cmd = new SqlCommand(sqlstr,conn);
  11.  
  12.     SqlDataReader dr = cmd.ExecuteReader();
  13.  
  14.     //第三:印出資料
  15.     while (dr.Read())
  16.     {
  17.         Response.Write("文章編號:" + dr["id"] + "<br>");
  18.         Response.Write("日  期:" + dr["test_time"] + "<br>");
  19.         Response.Write("文章標題:" + dr["title"]);
  20.         Response.Write("<hr>");
  21.     }
  22.  
  23.     //第四:關閉連線
  24.     cmd.Cancel();
  25.     dr.Close();
  26.     conn.Close();
  27.     conn.Dispose();
  28. }

標籤: ,

    

本篇文章引用網址:

其他隨機文章:
  • [WordPress plugins] 使用 Share This 在文章加入書籤功能
  • 道瓊單日跌幅777
  • 強制關閉處理程序的工具(NTSD)
  • [介紹] 直到這世界的盡頭 NordKapp 北角
  • 建立資料庫連接字串的方法



  • 寫下回應