public class MemoryHelper
{
/// <summary>
/// lcok obj
/// </summary>
private static readonly object obj = new object();
/// <summary>
///实例
/// </summary>
private static MemoryHelper _MemoryHelper { get; set; }
/// <summary>
/// SqlConnection
/// </summary>
private SQLiteConnection SQLCon { get; set; }
/// <summary>
/// Instance
/// </summary>
/// <returns></returns>
public static MemoryHelper Instance()
{
lock (obj)
{
if (_MemoryHelper == null)
{
lock (obj)
{
_MemoryHelper = new MemoryHelper();
}
}
}
return _MemoryHelper;
}
/// <summary>
/// Construct
/// </summary>
private MemoryHelper()
{
///内存数据库
SQLCon = new SQLiteConnection("Data Source=:memory:;Version=3; Cache Size = 3000;New=True;Pooling=False;Max Pool Size=100;LongNames = 0; Timeout = 1000; NoTXN = 0; SyncPragma = NORMAL; StepAPI = 0");
SQLCon.Open();
CreateTableInfo(SQLCon);
}
/// <summary>
/// 创建数据表
/// </summary>
/// <param name="con"></param>
private void CreateTableInfo(SQLiteConnection con)
{
string cmd;
cmd = @"CREATE TABLE table(
ID integer NOT NULL PRIMARY KEY AUTOINCREMENT,
name nvarchar(50))";
SQLiteHelper.ExecuteNonQuery(con, CommandType.Text, cmd, null);
}
/// <summary>
/// 删除questionInfo表行数据
/// </summary>
/// <returns></returns>
private void DelQuestionInfo(string strSql )
{
SQLiteHelper.ExecuteNonQuery(SQLCon, CommandType.Text, strSql);
}
/// <summary>
/// 查询题目数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public DataTable SelQuestionsInfoSql(string sql)
{
SQLiteCommand cmd = new SQLiteCommand();
CommandType cmdType = CommandType.Text;
PrepareCommand(cmd, null, cmdType, sql, null);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Parameters.Clear();
return ds.Tables[0];
}
public void UpdateDataTable(string sql)
{
SQLiteHelper.ExecuteNonQuery(SQLCon, CommandType.Text, sql);
}
/// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">SQLiteCommand object</param>
/// <param name="trans">SQLiteTransaction object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
/// <param name="cmdParms">SQLiteParameters to use in the command</param>
private void PrepareCommand(SQLiteCommand cmd, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms)
{
cmd.Connection = SQLCon;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (SQLiteParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}