sqlhelper 的使用 (C#)超级详细的入门教程

sql helper 的使用 (C#)小白教程

提到CRUD,很多刚入门的小白总是来一条写一条连接,先建立连接connection 打开连接 open 来查询query 最后别忘了关闭连接 close 。

要是一次写多个操作,那一遍一遍写下来肯定麻木了。

其实大神们早已写好数据库帮助类来简化这一操作了,使用SQLhelper。

那么如何使用呢?

首先创建sqlhelper类

在这里插入图片描述
输入SqlHelper 创建
在这里插入图片描述
粘贴这段代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DCsystem
{
    class SqlHelper
    { 
        public string strcon = @"data source=LAPTOP-GT9H3NIK;initial catalog=dc;integrated security=true";
        private SqlConnection con = null;
        private SqlConnection OpenOrCreateCon()
        {
            con = new SqlConnection(strcon);
            if (con != null && con.State == ConnectionState.Closed)
            { con.Open(); }
            return con;
        }
        private void ClosedCon()
        {
            if (con != null && con.State == ConnectionState.Open)
            { con.Close(); }

        }
        public int GetByScalar(string sql)//查询
        {
            OpenOrCreateCon();
            SqlCommand cmd = new SqlCommand(sql, con);
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            ClosedCon();
            return i;
        }
        public int GetByNonQuery(string sql)//增删改
        {
            OpenOrCreateCon();
            SqlCommand cmd = new SqlCommand(sql, con);
            int i = Convert.ToInt32(cmd.ExecuteNonQuery());
            ClosedCon();
            return i;
        }
        public int GetByNonQuery(string sql, SqlParameter[] para)
        {
            OpenOrCreateCon();
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddRange(para);
            int i = Convert.ToInt32(cmd.ExecuteNonQuery());
            ClosedCon();
            return i;
        }
        public SqlDataReader GetByReader(string sql)
        {
            OpenOrCreateCon();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader i = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return i;
        }
        public DataSet GetBySet(string sql)
        {
            OpenOrCreateCon();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }
        public DataTable GetByTable(string sql)
        {
            OpenOrCreateCon();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }
        public DataView GetByView(string sql)
        {
            OpenOrCreateCon();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            DataView dv = new DataView();
            dv.Table = dt;
            return dv;
        }
    }

}

注意namespace 后面的值DCsystem是自己的项目路径,不要写错了。
data source=数据库链接名,initial catalog = 数据库名。

 public string strcon = @"data source=LAPTOP-GT9H3NIK;initial catalog=dc;integrated security=true";

然后我们就可以在其他地方使用sqlhelper来节省我们的代码工作量啦!

下面是一个使用案例
在系统的登录页面有一个登录的功能按钮,这里需要查询数据。
在这里插入图片描述
在数据库查询代码上就可以写

 SqlHelper sqlhelper = new SqlHelper();
 String sql = "select count(*) from tb_User where UserName='"+textBox1.Text+"' and UserPwd='"+textBox2.Text+"'";
                    int i = sqlhelper.GetByScalar(sql);

只需要写sqlhelper.GetByScalar(查询语句) 就可以得到想要的结果,是不是很方便。

   private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
            else
            {
                if (textBox2.Text == "")
                {
                    MessageBox.Show("密码不能为空", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
                else
                {
                    SqlHelper sqlhelper = new SqlHelper();
                    String sql = "select count(*) from tb_User where UserName='"+textBox1.Text+"' and UserPwd='"+textBox2.Text+"'";
                    int i = sqlhelper.GetByScalar(sql);
                    if (i > 0)
                    {
                       
                        string sql1 = "select * from tb_User where UserName='"+textBox1.Text+"'";
                        SqlDataReader sdr = sqlhelper.GetByReader(sql1);
                        sdr.Read();
                        if (sdr.HasRows) 
                        {
                            Form2 f2 = new Form2();
                            f2.Name = textBox1.Text;
                            f2.Power = sdr["power"].ToString();
                            f2.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                            MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                            textBox1.Focus();
                            textBox1.Text = textBox2.Text = string.Empty;
                    } 
                }
            }
        }

最后也是直接登录成功进入到主页面中。
在这里插入图片描述

  • 8
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

RJGCWJH

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值