Sqlite執行單行sql指令
除了Firebird,另一個網路上大家推薦的Local DB - SQLite,
聽說效能更好,Firefox從3.0開始用SQLite做為資料庫。
以下是相關資料(其實網路上資源很多):
● 與.NET整合:[.Net]如何存取SQLite SQLite的.Net Provider SQLite on .NET
● 介紹:簡介SQLite SQLite使用教學
● 資源:Sqlite中文社區(簡體)
在開發專案時,採用了這套Local DB 來開發,針對單行指令做處理。
執行Select並傳回查詢值於DataTable
/// <summary>
/// 執行Select並傳回查詢值於DataTable
/// </summary>
/// <param name="SqlCmd">Sql指令</param>
/// <param name="SetName">表格名</param>
/// <returns>選取結果的DataTable</returns>
public static DataTable SelectQuery(string SqlCmd,string ConnectString)
{
IDbConnection dbcon;
dbcon = (IDbConnection)new SqliteConnection(ConnectString);
IDbCommand dbcmd = dbcon.CreateCommand();
dbcon.Open();
IDbTransaction trans = dbcon.BeginTransaction(); // 交易
dbcmd.Transaction = trans;
try
{
string sql = SqlCmd;
SqliteDataAdapter DataSel = new SqliteDataAdapter(sql, ConnectString);
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
DataTable TestA = new DataTable();
DataSel.Fill(TestA);
trans.Commit();
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
return TestA;
}
catch (Exception e)
{
trans.Rollback();
return null;
}
}
執行NonQuery指令
/// <summary>
/// 執行NonQuery指令
/// </summary>
/// <param name="SqlCmd">Sql指令</param>
public static void NonQueryCmd(string SqlCmd, string ConnectString)
{
IDbConnection dbcon;
dbcon = (IDbConnection)new SqliteConnection(ConnectString);
IDbCommand dbcmd = dbcon.CreateCommand();
dbcon.Open();
IDbTransaction trans = dbcon.BeginTransaction(); // 交易
dbcmd.Transaction = trans;
try
{
string sql = SqlCmd;
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
catch (Exception e)
{
trans.Rollback();
}
}