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;
}
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。