.net学习笔记3--登陆页面从数据库从获取用户信息进行登陆

1.建立一个test数据库
2.建立一张testUser表
在这里插入图片描述3.修改sa的密码
4.用sa登陆数据库
5.查询数据库代码

string strCon = "server=.;database=test;user=sa;pwd=123";
    void Login()
    {
        //数据库连接
        SqlConnection con = new SqlConnection(strCon);
        //con.ConnectionString = strCon;
        //sql语句,Connection对象,指令编辑
        //"+txtName.Text+"

        //string str1 = "1";
        //string str2 = "a";
        //string str3 = "b" + txtName.Text + txtPwd.Text;

        //string strSql = "select * from testUser where testName='" + txtName.Text + "'  and testPwd='" + txtPwd.Text + "' ";//该写法不提倡
        string strSql = "select * from testUser where testName=@name  and testPwd=@pwd ";//先编译再执行,存储过程,一组编译好的sql语句组成
        SqlCommand cmd = new SqlCommand(strSql, con);
        //Parameters参数集合
        cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);
        cmd.Parameters["@name"].Value = txtName.Text;
        cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value = txtPwd.Text;
        try
        {
            //con.Open();//打开数据库连接
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            //查询结果存放于DataTable中
            sda.Fill(dt);
            //Rows为DataTable的数据行的集合
            if (dt.Rows.Count > 0)
            {
                string code = this.TextBox3.Text;
                HttpCookie cookie = Request.Cookies["CheckCode"];
                if (code == cookie.Value.ToString())
                {
                    Response.Write("<script type='text/javascript'>alert('ok');</script>"); 
                }

                
            }
            else
            {
                Response.Write("<script type='text/javascript'>alert('error');</script>");
            }

            //con.Close();//关闭连接
        }
        catch
        {
            Response.Write("<script type='text/javascript'>alert('error');</script>");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值