• SqlHelper封装类测试


    SqlHelper封装类连接:https://www.cnblogs.com/Gzu_zb/p/10694207.html

    1.执行增、删、改的方法:ExecuteNonQuery

    public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
    {
        using (SqlConnection con = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                try
                {
                    return cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    return 0;//返回0表示操作失败
                             //throw;
                }
            }
        }
    }
    View Code

    更新:

    //更新
            int flag = SqlHelper.ExecuteNonQuery("UPDATE [dbo].[user] SET [password] = @password WHERE username=@username", new SqlParameter("@username", TextBox1.Text), new SqlParameter("@password", TextBox2.Text));
            if (flag != 0)
            {
                Response.Write("更新成功!");
            }
            else
            {
                Response.Write("更新失败!");
            }
    View Code

    插入:

    //插入
            int flag = SqlHelper.ExecuteNonQuery("Insert into [dbo].[user] values (@username,@password)", new SqlParameter("@username", TextBox1.Text), new SqlParameter("@password", TextBox2.Text));
            if (flag != 0)
            {
                Response.Write("插入成功!");
            }
            else
            {
                Response.Write("插入失败!");
            }
    View Code

    删除:

    //删除
            string sqlStrDel = "Delete from [dbo].[user] Where username=@username";
            int flag = SqlHelper.ExecuteNonQuery(sqlStrDel, new SqlParameter("@username", TextBox1.Text));
            if (flag != 0)
            {
                Response.Write("删除成功!");
            }
            else
            {
                Response.Write("删除失败!");
            }
    View Code

    2.执行查询多行多列的数据的方法:ExecuteReader

    public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
    {
        SqlConnection con = new SqlConnection(connStr);
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            if (pms != null)
            {
                cmd.Parameters.AddRange(pms);
            }
            try
            {
                con.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception)
            {
                con.Close();
                con.Dispose();
                throw;
            }
        }
    }
    View Code

    查询:

    string sqlStr = "select * from [dbo].[user]";
            SqlDataReader reader = SqlHelper.ExecuteReader(sqlStr);
            while (reader.Read())
            {
                Response.Write(reader[0] + "			" + reader[1] + "<br />");
            }
    View Code
    string sqlStr = "select password from [user] where username=@username";
            SqlDataReader reader = SqlHelper.ExecuteReader(sqlStr, new SqlParameter("@username", TextBox1.Text));
            while (reader.Read())
            {
                Response.Write(reader[0]);
            }
    View Code

    登录验证:

    string sqlStr = "select * from [dbo].[user] where username=@username and password=@password ";
            SqlDataReader reader = SqlHelper.ExecuteReader(sqlStr, new SqlParameter("@username", TextBox1.Text), new SqlParameter("@password", TextBox2.Text));
            if (reader.Read())
            {
                Label1.Text = "登录成功!";
            }
            else
            {
                Label1.Text = "用户名或密码错误!";
            }
    View Code

    3.封装一个执行返回单个对象的方法:ExecuteScalar()

    public static object ExecuteScalar(string sql, params SqlParameter[] pms)
    {
        using (SqlConnection con = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                con.Open();
                return cmd.ExecuteScalar();
            }
        }
    }
    View Code

    查询:

    //查找
            string sqlStrSel = "Select password From [dbo].[user] Where username=@username";
            try
            {
                pwd = SqlHelper.ExecuteScalar(sqlStrSel, new SqlParameter("@username", TextBox1.Text)).ToString();
                Label1.Text = pwd;
            }
            catch (Exception)
            {
                Label1.Text = "您查询的用户不存在!";
                //throw;
            }
    View Code

    4.执行返回DataTable的方法

    public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
    {
        DataTable dt = new DataTable();
        using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr))
        {
            if (pms != null)
            {
                adapter.SelectCommand.Parameters.AddRange(pms);
            }
            adapter.Fill(dt);
        }
        return dt;
    }
    View Code

    查询:

            string sqlStr = "select * from [user]";
            DataTable dt = SqlHelper.ExecuteDataTable(sqlStr);
    
            Label1.Text += dt.Rows.Count.ToString() + "<br/>";//获取从数据库得到数据的行数
    
            //遍历输出内存表中的所有数据
            //方式一
            foreach (DataRow s in dt.Rows)
            {
                //Label1.Text += s[0].ToString() + " " + s[1].ToString() + "<br/>";
                Label1.Text += s["username"].ToString() + " " + s["password"].ToString() + "<br/>";//这种写法与上一行的写法效果一样
            }
            //方式二
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //Label1.Text += dt.Rows[i][0] + " " + dt.Rows[i][1] + "<br/>";
                Label1.Text += dt.Rows[i]["username"] + " " + dt.Rows[i]["password"] + "<br/>";//这种写法与上一行的写法效果一样
            }
    View Code

     登录验证:

    string sqlStr = "select * from [user] where username=@username and password=@password";
            DataTable dt = SqlHelper.ExecuteDataTable(sqlStr,new SqlParameter("@username",TextBox1.Text),new SqlParameter("@password", TextBox2.Text));
            if (dt.Rows.Count == 1)
            {
                Label1.Text = "登录成功!";
            }
            else
            {
                Label1.Text = "用户名或密码错误!";
            }
    View Code

    附:

    所用数据库截图如下

     

    所用控件设置如下:

  • 相关阅读:
    不用轮子自己写滑动块
    let能否完全替代IIFE
    css3@media实现原理
    SQL 分组后获取每组中最大值
    如何在 JavaScript 中使用 C 程序
    响应式编程(Reactive Programming)(Rx)介绍
    图表(Chart & Graph)你真的用对了吗?
    JavaScript编码指南
    游戏心理学
    WannaCry结束了? 安专家注册域名掐断病毒传播
  • 原文地址:https://www.cnblogs.com/gzu_zb/p/10699924.html
Copyright © 2020-2023  润新知