ASP连接sqlsever数据库代码笔记

ASP.NET

查询并展示:

           SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);uid=sa ;pwd=; database=school";
            try
            {
                con.Open();
            }catch
            {
                Response.Write("数据库连接失败!");
                con.Close();
            }
            if(con.State==ConnectionState.Open)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select count(*) from tb_xinxi where stu_num='" + TextBox6.Text + "'";
                int i = Convert.ToInt32(cmd.ExecuteScalar());
                if (i == 1)
                {

                    SqlCommand cmd2 = new SqlCommand();
                    cmd2.Connection = con;
                    cmd2.CommandType = CommandType.Text;
                    cmd2.CommandText = "select * from tb_xinxi where stu_num='" + TextBox6.Text + "'";
                    SqlDataReader dr = cmd2.ExecuteReader();
                    while(dr.Read())
                    {
                        TextBox1.Text = (dr.GetString(0));
                        TextBox2.Text = (dr.GetString(1));
                        TextBox3.Text = (dr.GetString(2));
                        TextBox4.Text =Convert.ToString( (dr.GetValue(3)));
                        TextBox5.Text = (dr.GetString(4));
                        
                       
                    }
                }
            }
            else
            {
                Response.Write("<script>alert('记录不存在!')</script>");
                TextBox1.Focus();
            }

登录:

 SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;trusted_connection=true";
            try
            {
                con.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select count(*) from tb_user where user_pwd='" + TextBox1.Text + "' and user_pwd='" + TextBox2.Text + "'";
                int i = Convert.ToInt32(cmd.ExecuteScalar());
                if (i == 0)
                {
                    Response.Write("<script>alert('用户名错误,请重新出入!')</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
                else
                {
                    Response.Redirect("chaxun.aspx");
                }
            }
            catch {
                Response.Write("数据库连接失败123!");
                con.Close();
            }

注册:

SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;trusted_connection=true";
            try
            {
               
                if(TextBox1.Text!=""&&TextBox2.Text!="")
                {
                    con.Open();
                    SqlCommand cmd1 = new SqlCommand();
                    cmd1.Connection = con;
                    cmd1.CommandType = CommandType.Text;
                    cmd1.CommandText = "select count(*) from tb_user where user_name='" + TextBox1.Text + "'and user_pwd='" + TextBox2.Text + "'";
                    int i = Convert.ToInt32(cmd1.ExecuteScalar());
                    if (i == 1)
                    {
                        Response.Write("<script>alert('用户已经注册,请重新出入!')</script>");
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                    }
                    else
                    {
                       
                        SqlCommand cmd2 = new SqlCommand();
                        cmd2.Connection = con;
                        cmd2.CommandType = CommandType.Text;
                        cmd2.CommandText = "insert into tb_user values('"+TextBox1.Text+ "','" + TextBox2.Text + "')";
                        cmd2.ExecuteNonQuery();
                        con.Close();
                        Response.Write("<script>alert('注册成功!')</script>");
                        //TextBox1.Text = "";
                        //TextBox2.Text = "";
                    }

                }
               
            }
            catch
            {
                Response.Write("数据库连接失败!");
                con.Close();
            }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr小布懂

谢谢那么帅还对我这么好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值