码迷,mamicode.com
首页 > 数据库 > 详细

连接数据库

时间:2015-02-13 17:55:15      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

  1  protected void btnLogin_Click(object sender, EventArgs e)
  2         {
  3             #region first version
  4             string userName = this.txtUserName.Text.Trim();
  5             string passWord = this.txtPwd.Text.Trim();//删除字符串首尾的空白
  6             //获取用户信息
  7             if (rbtNoCard.Checked == true)
  8             {
  9                 //string strConn = ConfigurationManager.ConnectionStrings["LectureCheckedConnectionString"].ConnectionString;
 10                 string strConn = "server=(local);initial catalog=Change_SKJ;Integrated Security=SSPI";//亦可以以上方式连接数据库,但必须在web.config文件中设置appsettings
 11                 using (SqlConnection conn = new SqlConnection(strConn))
 12                 {
 13                     conn.Open();
 14                     using (SqlCommand cmd = conn.CreateCommand())//执行sql语句,无异于SqlCommand cmd = new SqlCommand(sql语句,数据库连接);
 15                     {
 16                         cmd.CommandText = "select * from T_CyUser where NoCard=@NoCard and Psw=@Psw";
 17                         cmd.Parameters.AddWithValue("NoCard", userName);//给前面的select语句中的@NoCard添加一个参数,值为userName,即为输入的用户名
 18                         cmd.Parameters.AddWithValue("Psw", passWord);
 19                         using (SqlDataReader dataReader = cmd.ExecuteReader())//ExecuteReader()执行命令,返回一个类型化的IDataReader
 20                         {
 21                             dataReader.Read();
 22                             if (dataReader.HasRows)//通过dataReader中是否包含行判断用户是否通过身份验证
 23                             {
 24                                 Session["card"] = 1;
 25                                 Session["Number"] = txtUserName.Text.Trim();
 26                                 //if ((int)dataReader["Attr"] == 1)
 27                                 //{
 28                                 //    Session["attr"] = 1;
 29                                 //    Session["IdMg"] = Convert.ToInt32(dataReader["ID"]);
 30                                 //    Response.Redirect("~/Teacher/Teacher.aspx");
 31                                 //}
 32                                 //else if ((int)dataReader["Attr"] == 0)
 33                                 //{
 34                                 //    Response.Redirect("~/User/Student.aspx");
 35                                 //}
 36                                 if ((int)dataReader["Attr"] == 2)
 37                                 {
 38                                     Session["attr"] = 2;
 39                                     Session["IdMg"] = Convert.ToInt32(dataReader["ID"]);
 40                                     Response.Redirect("~/SuperAdmin/Superadmin.aspx");
 41                                 }
 42                                 else
 43                                 {
 44                                     Response.Write("<script>alert(‘登录失败!请查看用户名或密码‘);location=‘Login.aspx‘</script>");//在asp.net中使用alert来提示错误信息
 45                                 }
 46                             }
 47                             else
 48                             {
 49                                 Response.Write("<script>alert(‘登录失败!请查看用户名或密码‘);location=‘Login.aspx‘</script>");//在asp.net中使用alert来提示错误信息
 50                             }
 51                         }
 52                     }
 53 
 54                 }
 55             }
 56             else if (rbtNoUser.Checked == true)
 57             {
 58                 //string strConn = ConfigurationManager.ConnectionStrings["LectureCheckedConnectionString"].ConnectionString;
 59                 string strConn = "server=(local);initial catalog=Change_SKJ;Integrated Security=SSPI";
 60                 using (SqlConnection conn = new SqlConnection(strConn))
 61                 {
 62                     conn.Open();
 63                     using (SqlCommand cmd = conn.CreateCommand())
 64                     {
 65                         cmd.CommandText = "select * from T_CyUser where NoUser=@NoUser and Psw=@Psw";
 66                         cmd.Parameters.AddWithValue("NoUser", userName);
 67                         cmd.Parameters.AddWithValue("Psw", passWord);
 68                         using (SqlDataReader dataReader = cmd.ExecuteReader())
 69                         {
 70                             dataReader.Read();
 71                             if (dataReader.HasRows)//通过dataReader中是否包含行判断用户是否通过身份验证
 72                             {
 73                                 Session["card"] = null;
 74                                 Session["Number"] = txtUserName.Text.Trim();
 75                                 if ((int)dataReader["Attr"] == 1)
 76                                 {
 77                                     Session["attr"] = 1;
 78                                     Session["IdMg"] = Convert.ToInt32(dataReader["ID"]);
 79                                     Response.Redirect("~/Teacher/Teacher.aspx");
 80                                 }
 81                                 else if ((int)dataReader["Attr"] == 0)
 82                                 {
 83                                     Response.Redirect("~/User/Student.aspx");
 84                                 }
 85                                 else 
 86                                 {
 87                                     Response.Write("<script>alert(‘重新选择身份‘);location=‘Login.aspx‘</script>");
 88                                 }
 89                             }
 90                             else
 91                             {
 92                                 Response.Write("<script>alert(‘登录失败!请查看用户名或密码‘);location=‘Login.aspx‘</script>");
 93                             }
 94                         }
 95                     }
 96 
 97                 }
 98             }
 99             #endregion
100         }

 

连接数据库

标签:

原文地址:http://www.cnblogs.com/Penfield/p/4290596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!