顾乔芝士网

持续更新的前后端开发技术栈

C#中较规范的一个读写数据库的类

新手可以来看看,高手可以自动忽略了,分享一个可以读写SQL Server数据库的类,以更好的实现对数据库的读写操作,适合于在公司里临时性做个小软件的朋友。

一、在C#中新建一个类,起个名字,然后直接复制代码

public static string connStr = "Server=服务器名或地址;Database=数据库名;User Id=sa;Password=密码";
/// <summary>
/// 根据SQL语句返回所查询的DataTable对像,有参数
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="param">SqlParameter参数</param>
/// <param name="type"></param>
/// <returns></returns>
public static DataTable GetTable(string sql, List<SqlParameter> param, CommandType type = CommandType.Text)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            if (param != null)
            {
                cmd.Parameters.AddRange(param.ToArray());
            }
            cmd.CommandType = type;
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
    return dt;
}
/// <summary>
/// 根据SQL语句返回所查询的DataTable对像,无参数
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="type"></param>
/// <returns></returns>
public static DataTable GetTable(string sql,CommandType type = CommandType.Text)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            cmd.CommandType = type;
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }
    return dt;
}
/// <summary>
/// 根据SQL语句查询只有一个返回结果的数据
/// </summary>
/// <param name="sql">查询的SQL语句</param>
/// <param name="type"></param>
/// <returns></returns>
public static string GetOneSQL(string sql, CommandType type = CommandType.Text)
{
    string mes="";
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            cmd.CommandType = type;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    mes = reader[0].ToString();
                }
            }
        }
    }
    return mes;
}
/// <summary>
/// 根据SQL语句查询只有一个返回结果的数据,只能用来查询count(*)等有明确返回结果且为数字的数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int GetOneSQL(string sql) 
{
    int num = 0;
    num = Convert.ToInt32(GetOneSQL(sql,CommandType.Text));
    return num;
}


/// <summary>
/// 根据SQL语句更新数据,返回受影响的行数,有参数
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="param">SqlParameter参数</param>
/// <param name="type"></param>
/// <returns></returns>
public static int UpdateDB(string sql, List<SqlParameter> param, CommandType type = CommandType.Text)
{
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            if (param != null)
            {
                cmd.Parameters.AddRange(param.ToArray());
            }
            cmd.CommandType = type;
            return cmd.ExecuteNonQuery();
        }
    }
}
/// <summary>
/// 根据SQL语句更新数据,返回受影响的行数
/// </summary>
/// <param name="sql">SQL语句</param>
/// <param name="type"></param>
/// <returns></returns>
public static int UpdateDB(string sql, CommandType type = CommandType.Text)
{
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand(sql, con))
        {
            con.Open();
            cmd.CommandType = type;
            return cmd.ExecuteNonQuery();
        }
    }
}

二、新建一个winform窗体,就可以直接使用了。

例如读取数据库中的表至DataGridView中,可以有下面两种方法(不用在意变量名称和数据库列名,举例用的

1、直接拼接SQL语句,但安全性差

 sql = "select 规范,数据,版本 from 画图规范 where 结构名称='" + lx + "' and 类型='" + name + "'";
 DG_show.DataSource = WJB.GetTable(sql);

2、使用结构化参数,安全性好

  sql = "select 规范,数据,版本 from 画图规范 where 结构名称=@lx and 类型=@name";
  List<SqlParameter> sp=new List<SqlParameter>();
  sp.Add(new SqlParameter("@lx",lx));
  sp.Add(new SqlParameter("@name",name));
  DG_show.DataSource = WJB.GetTable(sql,sp);

三、如果想查询某类型的合计数或者查有无重复,则可以使用下面的语句

 sql = "select count(*) as 记数 from 画图规范";
 int count=WJB.GetOneSQL(sql);
 MessageBox.Show(count.ToString());  //显示出来看一眼返回值是否正确


如果想要往数据库中增加或更改数据,则写好SQL语句后使用UpdateDB就可以了,是不是挺方便的,对于List<SqlParameter>来说不必事先定义它的大小,用的时候直接往里面增加就可以了。

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言