首页 > 数据库技术 > 详细

SQLHELPER FOR C#.NET

时间:2014-08-07 22:38:35      阅读:414      评论:0      收藏:0      [点我收藏+]

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;
    }
  }
}

}

SQLHELPER FOR C#.NET,布布扣,bubuko.com

SQLHELPER FOR C#.NET

原文:http://www.cnblogs.com/lschenblog/p/3897974.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!