ASP连接数据库登录按钮

(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();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值