public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/html; charset=utf-8”;
//前台传入参数
string name = context.Request.Params[“name”];
string pass = context.Request.Params[“pass”];
SqlConnection conn = new SqlConnection();
//数据库连接字符串
conn.ConnectionString = “Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\vs_project\testdb.mdf;Integrated Security=True;Connect Timeout=30”;
string commandText = “select top 1 * from userinfo where name=@name and pass=@pass”;
SqlCommand cmd = new SqlCommand(commandText, conn);
cmd.Parameters.Add(“name”, SqlDbType.NChar, 10).Value = name;
cmd.Parameters.Add(“pass”, SqlDbType.NChar, 10).Value = pass;
string str = “”;
try
{
conn.Open();
SqlDataReader thisSqlDataReader = cmd.ExecuteReader();
//读取查询数据
while (thisSqlDataReader.Read())
{
str += thisSqlDataReader[“id”] + “|” + thisSqlDataReader[“name”] + “|” + thisSqlDataReader[“pass”] + “|” + thisSqlDataReader[“roletype”];
}
conn.Close();
}
catch (Exception e)
{
context.Response.Write(e);
return;
}
if (!str.Equals(“”))
{
context.Response.Write(“恭喜您,登陆成功!” + str);
return;
}
context.Response.Write(“用户名或密码错误!”);
}
asp.net 连接数据库
最新推荐文章于 2023-12-27 19:22:42 发布