ASP串連資料庫登入按鈕

來源:互聯網
上載者:User

標籤:soft   user   config   rust   username   命名   ati   sql語句   net   

(1)在設定檔中加上以下代碼:

<connectionStrings>
<add name="accessconn" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\film.mdb;"/>
<add name="sqlconn" connectionString="Data Source=localhost;Initial Catalog=db_ASPNET;Trusted_Connection=SSPI;"/>
</connectionStrings>

到這裡就完成了第一步工作。

(2)在代碼檔案中引入節點檔案命名空間

using System.Configuration;

(3)在按鈕事件中編輯資料庫連接代碼

protected void Button1_Click(object sender, EventArgs e)//登入按鈕事件代碼,以access作為樣本,SQL以此類推
{
string str1 = ConfigurationManager.ConnectionStrings["accessconn"].ConnectionString.ToString(); //讀取configuration檔案的節點
//string str2 = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString.ToString();
OleDbConnection connstr1 = new OleDbConnection(str1); //資料庫連接對象
//SqlConnection connstr2 = new SqlConnection(str2);
if (connstr1.State == System.Data.ConnectionState.Closed) //開啟資料庫
{
connstr1.Open();
}
string content = "select * from user_info where uname=‘"+username.Text+"‘and upwd=‘"+userpwd.Text+"‘"; //編寫SQL語句
OleDbCommand commstr1 = new OleDbCommand(content, connstr1);//執行SQL語句
//SqlCommand commstr2 = new SqlCommand(content, connstr2);
OleDbDataReader dr = commstr1.ExecuteReader();//接受查詢結果
// SqlDataReader dr= commstr2.ExecuteReader();
if (dr.Read())//判斷查詢結果dr是否有資料
{
// Response.Redirect("welcome.aspx");//跳轉頁面

Response.Write("<script>alert(‘成功登陸‘);location.href=‘welcome.aspx‘;</script>");
}
else
{
Response.Write("<script>alert(‘使用者名稱或者密碼錯誤!‘)</script>");
}
dr.Close();
if (connstr1.State == System.Data.ConnectionState.Open) //關閉資料庫
{
connstr1.Close();
}
}

ASP串連資料庫登入按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.