...c# 数据库是sql server 2000 制作登陆界面谁能帮我做一个啊,是在...
发布网友
发布时间:2024-04-02 23:59
我来回答
共1个回答
热心网友
时间:2024-08-16 09:26
使用using () { ………………}可以避免try catch引起的代码量增多,而且using(){…………}之后,using区域内的所有资源,自动清除,不会暂用系统资源~~~
protected void Button1_Click(object sender, EventArgs e)
{
string yonghu = this.yonghu.Text.Trim();
string password = this.password.Text.Trim();
if (yonghu != "" && password != "")
{
string strConn = "data source=.;initial catalog=你的数据库名;Integrated Security =SSPI";
string strSql = string.Format("select count(*) from zhanghao where yonghu='{0}' ", yonghu);
string strSql2 = string.Format("select count(*) from zhanghao where yonghu='{0}' and password='{2}'", yonghu, password);
using (SqlConnection conn = new SqlConnection(strConn))
{
using (SqlCommand cmd = new SqlCommand(strSql, conn))
{
conn.Open();
string strNum = cmd.ExecuteScalar().ToString();
int Num = int.Parse(strNum) == 0 ? 0 : 1;
if (Num == 1)
{
cmd.CommandText = strSql2; //注意,这行的参数是 strSql2
cmd.Connection = conn;
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
this.Label1.Text = sdr["yonghu"].ToString();
Session["yonghu"] = sdr["yonghu"].ToString(); //使用Session保存用户名、密码,以便于其他页面使用
Session["password"] = sdr["password"].ToString();
Response.Redirect("另一个页.aspx");
}
}
}
else
{
Response.Write("<script>alert('无此记录或者用户名不存在~~~')</script>");
}
}
}
}
else
{
Response.Write("<script>alert('所有带 * 号输入框不能为空')</script>");
}
}