SQLHelper For C#.Net_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > SQLHelper For C#.Net

SQLHelper For C#.Net

 2014/7/11 10:54:07  CoderNull  程序员俱乐部  我要评论(0)
  • 摘要:publicclassSqliteHelper{//获取连接字符串privatestaticreadonlystringstr=ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;///<summary>///增删改功能///</summary>///<spname="sql">sql语句</sp>///<spname="sp">sql参数<
  • 标签:.net C# for net SQL

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

}

  • 相关文章
发表评论
用户名: 匿名