ExecuteScalar()操作后则会返回一个首行首列的数据
#region 登录ExecuteScalar
//连接数据库
string sql = "Data Source=WIN-SVM0RQ029SM;Initial Catalog=master;Integrated Security=True";
private static string Consql = ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString;
string user = txtuser.Text.Trim();
string pwd = txtpwd.Text;
//string consql = string.Format("select * from [User] where UserName='{0}' and Password='{1}'", user, pwd);
//SQL语句
string sql = "select *from [User] where[UserName] = @UserName and[Password] = @Password";
SqlParameter[] parm = {
new SqlParameter( "@UserName",user),
new SqlParameter( "@Password" , pwd)
};
//Connection开启程序数据库之间的链接
using (SqlConnection con = new SqlConnection(Consql))
{
//DataAdapter数据源以及DateSet之间执行数据传输的工作
using (SqlCommand com = new SqlCommand(sql, con))
{
com.Parameters.AddRange(parm);
//打开连接
con.Open();
//执行操作
int result = Convert.ToInt32(com.ExecuteScalar());
if (result>0)
{
Session["name"]= txtuser.Text;
Response.Redirect("开票界面.aspx");
}
else
{
Response.Write("<script>alert('密码或用户名不正确')</script>");
txtuser.Text = "";
txtpwd.Text = "";
}
ExecuteReader() 操作后则返回ExecuteReader对象一次一笔向下循序的读取数据库中的数据
//连接数据库
//string sql = "Data Source=WIN-SVM0RQ029SM;Initial Catalog=master;Integrated Security=True";
private static string Consql = ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString;
string acc = txtuser.Text.Trim();
string pwd = txtpwd.Text;
//string consql = string.Format("select * from [User] where UserName='{0}' and Password='{1}'", user, pwd);
//SQL语句 参数化查询
string sql = "select * from [User] where[Account] = @Account and[Password] = @Password";
SqlParameter[] parm = {
new SqlParameter( "@Account",acc),
new SqlParameter( "@Password" , pwd)
};
//Connection开启程序数据库之间的链接
using (SqlConnection con = new SqlConnection(Consql))
{
//DataAdapter数据源以及DateSet之间执行数据传输的工作
using (SqlCommand com = new SqlCommand(sql, con))
{
com.Parameters.AddRange(parm);
//打开连接
con.Open();
//执行操作
//SqlDataReader连线式数据库
using (SqlDataReader reader = com.ExecuteReader())
{
//如果游标有数据即可登陆成功
if (reader.HasRows)
{
//游标前进一行拿到登陆数据
if (reader.Read())
{
//Session["name"] = txtuser.Text;
//索引获取username的值
//string username = reader.GetString(1);
// Session["name"] = name;
//按照名字直接获取值
string username = reader["UserName"].ToString();
//将获取得值传给Session
Session["name"] = username;
Response.Redirect("开票界面.aspx");
}
}
else
{
Response.Write("<script>alert('密码或用户名不正确')</script>");
txtuser.Text = "";
txtpwd.Text = "";
}
}
}
}
SqlDataAdapter()主要是在数据库和DataSet执行数据传输,可以透过Command对象下达命令
string acc = txtuser.Text.Trim();
string pwd = txtpwd.Text; //string consql = string.Format("select * from [User] where UserName='{0}' and Password='{1}'", user, pwd); //SQL语句 参数化查询 string sql = "select * from [User] where[Account] = @Account and[Password] = @Password"; SqlParameter[] parm = { new SqlParameter( "@Account",acc), new SqlParameter( "@Password" , pwd) }; //创建SqlDataAdapter适配器 using (SqlDataAdapter sda = new SqlDataAdapter(sql, Consql)) { //取出SqlDataAdapter的值 sda.SelectCommand.Parameters.AddRange(parm); //创建DataTable表 DataTable dt = new DataTable(); //将数据填充到表单中 sda.Fill(dt); //如果表单数据长度大于1 即登陆成功 if (dt.Rows.Count > 0) { //获取表单的第一条数据 DataRow dr = dt.Rows[0]; string username = dr["UserName"].ToString(); //将获取得值传给Session Session["name"] = username; Response.Redirect("开票界面.aspx"); } else { Response.Write("<script>alert('密码或用户名不正确')</script>"); txtuser.Text = ""; txtpwd.Text = ""; } } }