SQLHelper For C#.Net

public class SqliteHelper
{
  //获取连接字符串
  private static readonly string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
  /// <summary>
  /// 增删改功能
  /// </summary>
  /// <sp name="sql">sql语句</sp>
  /// <sp name="sp">sql参数</sp>
  /// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string sql, params SQLiteParameter[] sp)
{
  using (SQLiteConnection con=new SQLiteConnection(str))
  {
    using (SQLiteCommand cmd=new SQLiteCommand(sql,con))
    {
      con.Open();
      if (param!=null)
      {
        cmd.Parameters.AddRange(sp);
      }
      return cmd.ExecuteNonQuery();
    }
  }
}
  /// <summary>
  /// 查询
  /// </summary>
  /// <sp name="sql">sql语句</sp>
  /// <sp name="param">参数</sp>
  /// <returns>返回首行首列</returns>
public static object ExecuteScalar(string sql, params SQLiteParameter[] sp)
{
  using (SQLiteConnection con=new SQLiteConnection(str))
  {
    using (SQLiteCommand cmd=new SQLiteCommand(sql,con))
    {
      con.Open();
      if (param!=null)
      {
        cmd.Parameters.AddRange(sp);
      }
      return cmd.ExecuteScalar();
    }
  }
}
  /// <summary>
  /// 查询表
  /// </summary>
  /// <sp name="sql">sql语句</sp>
  /// <sp name="param">参数</sp>
  /// <returns>返回table</returns>
public static DataTable ExecuteTable(string sql, params SQLiteParameter[] sp)
{
  DataTable dt = new DataTable();
  using (SQLiteDataAdapter sda=new SQLiteDataAdapter(sql,str))
  {
    if (param!=null)
    {
      sda.SelectCommand.Parameters.AddRange(sp);
    }
    sda.Fill(dt);
  }
  return dt;
}
  /// <summary>
  /// 查询
  /// </summary>
  /// <sp name="sql">sql语句</sp>
  /// <sp name="param">参数</sp>
  /// <returns>数据</returns>
public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] sp)
{
  SQLiteConnection con = new SQLiteConnection(str);
  using (SQLiteCommand cmd=new SQLiteCommand(sql,con))
  {
    if (param!=null)
    {
      cmd.Parameters.AddRange(sp);
    }
    try
    {
      con.Open();
      return cmd.ExecuteReader(CommandBehavior.CloseConnection);
    }
    catch (Exception ex)
    {
      con.Close();
      con.Dispose();
      throw ex;
    }
  }
}

}

你可能感兴趣的:(.net)