用asp.net连接数据库,做登录页面,怎么弄呢?
办法:
//把上面的代码放在login.aspx.cs里面,你哪里登陆就调用这个函数,传登陆账号跟密码进来就行了.
//登陆成功就返回true,否则就返回false
//登陆成功你可以用下列语句跳转到你那个页面
Response.Redirect("里面你跳转的Url");
//登陆失败你可以提示
Response.Write("<script> alert('账号密码错误');</
办法:
using System.Data.SqlClient;//记得加引用
private bool Login(string sno,string sname)
{
SqlConnection con = new SqlConnection("Data Source=数据库连接;Initial Catalog=Stu_table;User ID=sa;Password=123456789");
con.Open();
string sql = string.Format("select count(*) from Student where Sno='{0}' and Sname='{1}'",sno,sname);//这里你自己改改了,不知道你用什么登陆的,账号密码不知道你用什么字段,暂时认为你用学号跟姓名登陆
SqlCommand com = new SqlCommand(sql, con);
int flag =(int) com.ExecuteScalar();
con.Close();
if (flag > 0)
{
return true;
}
return false;
}
//把上面的代码放在login.aspx.cs里面,你哪里登陆就调用这个函数,传登陆账号跟密码进来就行了.
//登陆成功就返回true,否则就返回false
//登陆成功你可以用下列语句跳转到你那个页面
Response.Redirect("里面你跳转的Url");
//登陆失败你可以提示
Response.Write("<script> alert('账号密码错误');</