欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# SQL server数据库基本操作(连接、增、删、改、查)封装

程序员文章站 2023-11-13 12:01:04
将C#对SQLserver常用的操作进行封装,方便后续开发使用! ......

 写在前面:

在日常的工作中,通常一个项目会大量用的数据库的各种基本操作,因此小编几个常见的数据库的操作封装成了一个dll方便后续的开发使用。sqlserver数据库是最为常见的一种数据库,本文则主要是记录了c#对sql的连接、增、删、改、查的基本操作,如有什么问题还请各位大佬指教。后续也将对其他几个常用的数据库进行相应的整理。话不多说,直接开始码代码。

先声明一个sqlconnection便于后续使用。

private sqlconnection sql_con;//声明一个sqlconnection

 

 sql打开:

/// <summary>
/// sqlserver open
/// </summary>
/// <param name="link">link statement</param>
/// <returns>success:success; fail:reason</returns>
public string sqlserver_open(string link)
{
  try
  {
    sql_con = new sqlconnection(link); 
    sql_con.open();
    return "success";
  }
  catch (exception ex)
  {
    return ex.message;
  }
}

sql关闭:

/// <summary>
/// sqlserver close
/// </summary>
/// <returns>success:success fail:reason</returns>
public string sqlserver_close()
{
  try
  {
    if (sql_con == null)
    {
      return "no database connection";
    }
    if (sql_con.state == connectionstate.open || sql_con.state == connectionstate.connecting)
    {
      sql_con.close();
      sql_con.dispose();
    }
    else
    {
      if (sql_con.state == connectionstate.closed)
      {
             return "success";
      }
      if (sql_con.state == connectionstate.broken)
      {
        return "connectionstate:broken";
      }
    }
    return "success";
  }
  catch (exception ex)
  {
    return ex.message;
  }
}

sql的增删改:

/// <summary>
/// sqlserver insert,delete,update
/// </summary>
/// <param name="sql">insert,delete,update statement</param>
/// <returns>success:success + number of affected rows; fail:reason</returns>
public string sqlserver_insdelupd(string sql)
{
  try
  {
    int num = 0;
    if (sql_con == null)
    {
      return "please open the database connection first";
    }
    if (sql_con.state == connectionstate.open)
    {
      sqlcommand sqlcommand = new sqlcommand(sql, sql_con);
      num = sqlcommand.executenonquery();
    }
    else
    {
      if (sql_con.state == connectionstate.closed)
      {
        return "database connection closed";
      }
      if (sql_con.state == connectionstate.broken)
      {
        return "database connection is destroyed";
      }
      if (sql_con.state == connectionstate.connecting)
      {
        return "the database is in connection";
      }
    }
    return "success" + num;
  }
  catch (exception ex)
  {
    return ex.message.tostring();
  }
}

sql的查:

/// <summary>
/// sqlserver select
/// </summary>
/// <param name="sql">select statement</param>
/// <param name="record">success:success; fail:reason</param>
/// <returns>select result</returns>
public dataset sqlserver_select(string sql, out string record)
{
  try
  {
    dataset dataset = new dataset();
    if (sql_con == null)
    {
      record = "please open the database connection first";
        return dataset;
      }
       if (sql_con.state == connectionstate.open)
    {
      sqldataadapter sqldataadapter = new sqldataadapter(sql, sql_con);
      sqldataadapter.fill(dataset, "sample");
      sqldataadapter.dispose();
      record = "success";
      return dataset;
    }
    if (sql_con.state == connectionstate.closed)
    {
      record = "database connection closed";
      return dataset;
    }
    if (sql_con.state == connectionstate.broken)
    {
        record = "database connection is destroyed";
      return dataset;
    }
    if (sql_con.state == connectionstate.connecting)
    {
      record = "the database is in connection";
      return dataset;
    }
    record = "error";
    return dataset;
  }
  catch (exception ex)
  {
    dataset dataset = new dataset();
    record = ex.message.tostring();
    return dataset;
  }
}

 小编发现以上这种封装方式还是很麻烦,每次对sql进行增删改查的时候还得先打开数据库,最后还要关闭,实际运用起来比较麻烦。因此对上面两个增删改查的方法进行了重载,在每次进行操作时都先打开数据库,然后关闭数据库。

        /// <summary>
        /// sqlserver insert,delete,update
        /// </summary>
        /// <param name="sql">insert,delete,update statement</param>
        /// <param name="link">link statement</param>
        /// <returns>success:success + number of affected rows; fail:reason</returns>
        public string sqlserver_insdelupd(string sql, string link)
        {
            try
            {
                int num = 0;
                using (sqlconnection con = new sqlconnection(link))
                {
                    con.open();                    
                    sqlcommand cmd = new sqlcommand(sql, con);  
                    num = cmd.executenonquery();            
                    con.close();
                    return "success" + num;
                }
            }
            catch (exception ex)
            {
                return ex.message.tostring();
            }
        }

 

        /// <summary>
        /// sqlserver select
        /// </summary>
        /// <param name="sql">select statement</param>
        /// <param name="link">link statement</param>
        /// <param name="record">success:success; fail:reason</param>
        /// <returns>select result</returns>
        public dataset sqlserver_select(string sql, string link, out string record)
        {
            try
            {
                dataset ds = new dataset();
                using (sqlconnection con = new sqlconnection(link))
                {
                    con.open();                                         
                    sqldataadapter sda = new sqldataadapter(sql, con);  
                    sda.fill(ds, "sample");                            
                    con.close();                                       
                    sda.dispose();                                      
                    record = "success";
                    return ds;
                }
            }
            catch (exception ex)
            {
                dataset dataset = new dataset();
                record = ex.message.tostring();
                return dataset;
            }
        }

小编只是对简单的知识进行整理,通大家分享并方便自己查看,并无恶意,如有侵权,联系小编,小编将立即删除,谢谢。