连接数据库

在连接之前,我们先在数据库中建立数据表以及待会要用到的数据

创建连接数据库的类

右击项目->添加新项


选择类

点击是

创建好的类如下

连接数据库呢我们肯定需要用到一些关于数据库的类。
首先我们要引入命名空间using System.Data.SqlClient;

然后,网站连接数据库并且操作一共需要三步:连接、准备执行、执行
我们在这个类里面写前两步

连接

数据库的连接字符串,可以直接复制数据库的连接字符串Data Source=.\SQLEXPRESS;AttachDbFilename=D:\201717010009\Solution5\web2\App_Data\Database.mdf;Integrated Security=True;User Instance=True
但是要在前面加上一个@ 反转义
也可以把中间路径替换成相对路径 =|DataDirectory|\Database.mdf;

    public static SqlConnection con()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "这里写数据库的连接字符串";
        conn.Open();
        return conn;
    }

准备执行

    public static SqlCommand comd(string sql)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con();
        cmd.CommandText = sql;//传入的sql语句
        return cmd;
    }

执行登录操作

比如说我们在前端写一个登陆页面
那么输入用户名和密码之后 我们点击登陆按钮 就要从数据库去查找匹配输入的信息是否正确
那我们在登陆按钮里写上动态代码

    protected void Button1_Click(object sender, EventArgs e)
    {
        string username=   ;//赋值为前端输入的用户名
        string password=   ;//赋值为前端输入的密码
        //存下sql语句
        string sql="select *from tbuser where username='"+username+"' and password='"+password+"'";
        //打开并连接数据库
        SqlCommand cmd=new SqlCommand();
        //传入sql语句 这个DB是刚才创建的连接数据库的类名 comd是他里面的准备执行的方法名
        cmd=DB.comd(sql);
        //从数据库中读取数据
        SqlDataReader sdr=cmd.ExecuteReader();
        //判断是否读到数据
        if(sdr.Read())
        {
            //输出查询到的数据 sdr[""]里面写的是数据库中表里的字段名称
            Response.Write(sdr["username"].ToString());
            //Response.Redirect("index.aspx");//页面重定向 (跳转页面)
        }
        else
        {
            Response.Write("<script>alert('用户名或密码错误')</script>");
        }
    }

跳转欢迎界面

然后我们再实现一个功能。
当用户密码和用户名输入正确的时候,我们跳转到欢迎页面去,并且输入一句话“xxx,欢迎你!”

		if(sdr.Read())//开始读取数据
        {
            //将读取到的字段数据 存下来 全部网页通用
            Session["u"] = sdr["username"].ToString();
            //跳转到index.asp页面
            Response.Redirect("index.aspx");//页面重定向 (跳转页面)
        }

而在index.asp页面 我们写上动态代码
让他打印出欢迎语句

	protected void Page_Load(object sender, EventArgs e)
    {
        string s=Session["u"]+",欢迎你!";
        Response.Write(s);
    }

实现注册操作

注册和登录差不多 但是不一样的就是 在注册之前我们需要先查询一下用户名是否已经存在了

protected void Button1_Click(object sender, EventArgs e)
    {
        string username= usernameTextBox.Text ;
        string password= passwordTextBox.Text ;
        string tel = telTextBox.Text;

        SqlCommand cmd = new SqlCommand();//打开数据库

        if (check(username, cmd))
        {
            string sql = "insert into tbuser(username,password,phone) values('" + username + "','" + password + "','" + tel + "')";
            cmd = Class1.comd(sql);
            int i=cmd.ExecuteNonQuery();//读取数据库受影响行数
            if(i>0)  Response.Write("<script>alert('注册成功')</script>");
            else     Response.Write("<script>alert('注册失败')</script>");

        }
        else
        {
            Response.Write("<script>alert('用户名已存在')</script>");
        }
    }
    //查询用户名是否已经存在
    protected bool check(string u,SqlCommand cmd) 
    {
        string sql="select *from tbuser where username='"+u+"'";
        cmd = Class1.comd(sql);
        SqlDataReader sdr = cmd.ExecuteReader();
        if (sdr.Read())
        {
            return false;
        }
        else 
        {
            return true;
        }
    }