調用數據連接兩種方法


1.web.config (配置文件) 中
<appSettings>
<add key="ConnectionString" value="serverlocalhost\sqlexpress;uid=sa;pwd=123456;database=News"/>
</appSettings>
調用的時候
string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection Conn = new SqlConnection(strConn);
2.或者不用web.config直接在文件中寫
SqlConnection conn = new SqlConnection("server=.\\SQLEXPRESS;uid=sa;pwd=123456;database=login");
如何是Express版的數據庫,一定要在服務器名的後面加上[url=file://%20%20ssqlexpress/]\\SSQLEXPRESS[/url]
一個完整的例子
  string userName = Request.Form["userName"];
         string userPwd = Request.Form["userPwd"];
         SqlConnection con = new SqlConnection("server=localhost\\SqlExpress;uid=sa;pwd=123456;database=login");
         con.Open();
         SqlCommand cmd=new SqlCommand("select count(*) from login where userName='"+userName+"' and userPwd='"+userPwd+"'",con);
         int count=Convert.ToInt32(cmd.ExecuteScalar());
         if(count>0)
         {
         Response.Redirect("main.aspx");
         }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章