using System.Collections.Generic
using System.Linq
using System.Text
using System.Data
using System.Data.SQLite
namespace DAL
{
static class SqlHelper
{
private static bool Conn = false
private static string ConnStr = @"Data Source=FileNamePooling=trueFailIfMissing=false"
static SqlHelper()
{
Conn = TestConn()
}
public static bool Update(string SQL, params SqlParameter[] SqlParameters)
{
bool Result = false
if (Conn)
using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))
{
SqlConn.Open()
using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn))
{
if (SQLiteParameters != null)
SqlCmd.Parameters.AddRange(SqlParameters)
Result = SQLiteCmd.ExecuteNonQuery() >0
}
}
return Result
}
public static T Scalar<T>(string SQL, params SQLiteParameter[] SqlParameters)
{
object Result = null
if (Conn)
using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))
{
SqlConn.Open()
using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn))
{
if (SQLiteParameters != null)
SqlCmd.Parameters.AddRange(SqlParameters)
Result = SqlCmd.ExecuteScalar()
}
}
return (Result != null) ? (T)Result : default(T)
}
public static DataTable Query(string SQL, params SQLiteParameter[] SqlParameters)
{
DataTable Result = null
if (Conn)
using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))
{
SqlConn.Open()
using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn))
{
if (SqlParameters != null)
SqlCmd.Parameters.AddRange(SqlParameters)
using (SQLiteDataAdapter SqlAdap = new SQLiteDataAdapter(SqlCmd))
using (DataSet ds = new DataSet())
{
SqlAdap.Fill(ds)
Result = ds.Tables[0]
}
}
}
return Result
}
private static bool TestConn()
{
bool TestResult = false
try
{
using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr))
SqlConn.Open()
TestResult = true
}
catch
{
TestResult = false
}
return TestResult
}
}
}
using Systemusing System.Collections.Genericusing System.Linqusing System.Textusing System.Datausing System.Data.SQLitenamespace DAL { static class SqlHelper { private static bool Conn = falseprivate static string ConnStr = @"Data Source=FileNamePooling=trueFailIfMissing=false"static SqlHelper() { Conn = TestConn()} public static bool Update(string SQL, params SqlParameter[] SqlParameters) { bool Result = falseif (Conn) using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) { SqlConn.Open()using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn)) { if (SQLiteParameters != null) SqlCmd.Parameters.AddRange(SqlParameters)Result = SQLiteCmd.ExecuteNonQuery() >0} } return Result} public static T Scalar<T>(string SQL, params SQLiteParameter[] SqlParameters) { object Result = nullif (Conn) using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) { SqlConn.Open()using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn)) { if (SQLiteParameters != null) SqlCmd.Parameters.AddRange(SqlParameters)Result = SqlCmd.ExecuteScalar()} } return (Result != null) ? (T)Result : default(T)} public static DataTable Query(string SQL, params SQLiteParameter[] SqlParameters) { DataTable Result = nullif (Conn) using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) { SqlConn.Open()using (SQLiteCommand SqlCmd = new SQLiteCommand(SQL, SqlConn)) { if (SqlParameters != null) SqlCmd.Parameters.AddRange(SqlParameters)using (SQLiteDataAdapter SqlAdap = new SQLiteDataAdapter(SqlCmd)) using (DataSet ds = new DataSet()) { SqlAdap.Fill(ds)Result = ds.Tables[0]} } } return Result} private static bool TestConn() { bool TestResult = falsetry { using (SQLiteConnection SqlConn = new SQLiteConnection(ConnStr)) SqlConn.Open()TestResult = true} catch { TestResult = false} return TestResult} } }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)