asp 连接数据库的语句
时间:2011-06-01 来源:nbfhxy
#region 连接数据库
SqlConnection conn = new SqlConnection();
//conn.ConnectionString = "server=127.0.0.1\\sqlexpress;uid=sa;pwd=0;database=EXTDatabase";
conn.ConnectionString = ConfigurationManager.ConnectionStrings["stringcon"].ConnectionString;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format(" select * from login where USERID = '{0}' and PASSWORD = '{1}'", UserName, Password);
//object obj = cmd.ExecuteScalar();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
Session["rolevalue"] = reader["RoleValue"].ToString();
Session["username"] = reader["USERID"].ToString();
string struser=Session["username"].ToString();
if (Session["username"] != "" && Session["username"] != null)
Response.Write("{success:true}");
else
Response.Write("{success:false}");
}
else
{
Response.Write("{success:false}");
}
if (conn.State != ConnectionState.Closed)
conn.Close();
#endregion
#region 连接数据库
SqlConnection conn = new SqlConnection();
//conn.ConnectionString = "server=127.0.0.1\\sqlexpress;uid=sa;[email protected];database=EXTDatabase";
conn.ConnectionString = ConfigurationManager.ConnectionStrings["stringcon"].ConnectionString;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = string.Format(" select * from login where USERID = '{0}' and PASSWORD = '{1}'", UserName, Password);
//object obj = cmd.ExecuteScalar();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
Session["rolevalue"] = reader["RoleValue"].ToString();
Session["username"] = reader["USERID"].ToString();
string struser=Session["username"].ToString();
if (Session["username"] != "" && Session["username"] != null)
Response.Write("{success:true}");
else
Response.Write("{success:false}");
}
else
{
Response.Write("{success:false}");
}
if (conn.State != ConnectionState.Closed)
conn.Close();
#endregion
相关阅读 更多 +