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

 写在前面:

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

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

private SqlConnection sql_con;//声明一个SqlConnection

 

 sql打开:

/// 
/// SQLserver open
/// 
/// link statement
/// Success:success; Fail:reason
public string Sqlserver_Open(string link)
{
  try
  {
    sql_con = new SqlConnection(link); 
    sql_con.Open();
    return "success";
  }
  
catch (Exception ex)
  {
    
return ex.Message;
  }
}

sql关闭:

/// 
/// SQLserver close
/// 
/// Success:success Fail:reason
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的增删改:

/// 
/// SQLserver insert,delete,update
/// 
/// insert,delete,update statement
/// Success:success + Number of affected rows; Fail:reason
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的查:

/// 
/// SQLserver select
/// 
/// select statement
/// Success:success; Fail:reason
/// select result
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进行增删改查的时候还得先打开数据库,最后还要关闭,实际运用起来比较麻烦。因此对上面两个增删改查的方法进行了重载,在每次进行操作时都先打开数据库,然后关闭数据库。

        /// 
        /// SQLserver insert,delete,update
        /// 
        /// insert,delete,update statement
        /// link statement
        /// Success:success + Number of affected rows; Fail:reason
        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();
            }
        }

 

        /// 
        /// SQLserver select
        /// 
        /// select statement
        /// link statement
        /// Success:success; Fail:reason
        /// select result
        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;
            }
        }

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

 

 

 

 

你可能感兴趣的:(C# SQL server数据库基本操作(连接、增、删、改、查)封装)