前端之家收集整理的这篇文章主要介绍了
簡單SQLite 數據庫操作Demo,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class DbHelpersqlite
{
public string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + @"WIP.db;Version=3;";
public event EventHandler<sqliteMessageEventArg> sqliteMessageEvent;
public DbHelpersqlite()
{ }
/// <summary>
/// 執行單條sql語句,返回值(bool)
/// </summary>
/// <param name="sqlString"></param>
/// <returns></returns>
public bool Executesql(string sqlString)
{
var eventHandle = sqliteMessageEvent;
string msg = "";
using (sqliteConnection connection = new sqliteConnection(connectionString))
{
using (sqliteCommand cmd = new sqliteCommand(sqlString,connection))
{
try
{
connection.Open();
cmd.ExecuteNonQuery();
msg = "sql Command: " + sqlString + " Excute Success!";
if (eventHandle != null)
{
eventHandle(null,new sqliteMessageEventArg(sqliteMessageType.Msg,msg,sqlString));
}
return true;
}
catch (sqliteException ex)
{
msg = "Execute sql Error Message: " + ex.Message;
if (eventHandle != null)
{
eventHandle(null,new sqliteMessageEventArg(sqliteMessageType.Errorsql,sqlString));
}
return false;
}
finally
{
connection.Close();
cmd.Dispose();
}
}
}
}
/// <summary>
/// 獲取最大值,返回值(int)
/// </summary>
/// <param name="FieldName">欄位名稱</param>
/// <param name="TableName">表名稱</param>
/// <returns></returns>
public int GetMaxID(string FieldName,string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
return int.Parse(obj.ToString());
}
/// <summary>
/// 單值查詢,返回查詢結果(Object)
/// </summary>
/// <param name="sqlString"></param>
/// <returns></returns>
public object GetSingle(string sqlString)
{
var eventHandle = sqliteMessageEvent;
string msg = "";
using (sqliteConnection connection = new sqliteConnection(connectionString))
{
using (sqliteCommand cmd = new sqliteCommand(sqlString,connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj,null)) || (Object.Equals(obj,System.DBNull.Value)))
{
msg = "WARN : sql Command Return null! [ " + sqlString + "]";
if (eventHandle != null)
{
eventHandle(null,sqlString));
}
return null;
}
else
{
msg = "sql Command: " + sqlString + " Excute Success!";
if (eventHandle != null)
{
eventHandle(null,sqlString));
}
return obj;
}
}
catch (sqliteException e)
{
msg = "Execute sql Error Message: " + e.Message;
if (eventHandle != null)
{
eventHandle(null,sqlString));
}
connection.Close();
return null;
}
finally
{
connection.Close();
cmd.Dispose();
}
}
}
}
/// <summary>
/// 執行多條sql語句,實現數據庫事務
/// </summary>
/// <param name="sqlStringList"></param>
public void ExcutesqlTran(ArrayList sqlStringList)
{
var eventHandle = sqliteMessageEvent;
string msg = "";
using (sqliteConnection conn = new sqliteConnection(connectionString))
{
conn.Open();
sqliteCommand cmd = new sqliteCommand();
cmd.Connection = conn;
sqliteTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < sqlStringList.Count; n++)
{
string strsql = sqlStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
msg = "sql Command: " + strsql + " Excute Success!";
if (eventHandle != null)
{
eventHandle(null,strsql));
}
}
else
{
msg = "Execute sql Error Message: strsql.Trim().Length=" + strsql.Trim().Length.ToString();
if (eventHandle != null)
{
eventHandle(null,strsql));
}
}
}
tx.Commit();
}
catch (sqliteException ex)
{
tx.Rollback();
msg = "Execute sql Error Message: " + ex.Message;
if (eventHandle != null)
{
eventHandle(null,"[ExcutesqlTran]"));
}
}
finally
{
conn.Close();
cmd.Dispose();
}
}
}
/// <summary>
/// 執行單條sql語句,返回值(DataTable)
/// </summary>
/// <param name="sqlString"></param>
/// <returns></returns>
public DataTable SelectUse(string sqlString)
{
var eventHandle = sqliteMessageEvent;
string msg = "";
sqliteCommand cmd = null;
sqliteDataAdapter oda = null;
sqliteConnection connection = null;
DataTable dt;
try
{
connection = new sqliteConnection(connectionString);
cmd = new sqliteCommand(sqlString,connection);
connection.Open();
dt = new DataTable();
cmd.Transaction = connection.BeginTransaction();
oda = new sqliteDataAdapter(cmd);
oda.Fill(dt);
msg = "sql Command: " + sqlString + " Excute Success!";
if (eventHandle != null)
{
eventHandle(null,sqlString));
}
return dt;
}
catch (sqliteException ex)
{
msg = "Execute sql Error Message: " + ex.Message;
if (eventHandle != null)
{
eventHandle(null,sqlString));
}
return null;
}
finally
{
connection.Close();
cmd.Dispose();
oda.Dispose();
}
}
/// <summary>
/// 測試DB連接
/// </summary>
/// <returns></returns>
public bool ConnectConfirm()
{
var sqlStr = "select 1+1 from A"; //A是sqlite DB中的表
var result = SelectUse(sqlStr);
if (result != null)
{
if (result.Rows.Count == 0)
{
return false;
}
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 回復事件
/// </summary>
public class sqliteMessageEventArg:EventArgs
{
public sqliteMessageType EventId { get; private set; }
public string EventData{get;private set;}
public string sqlCommand { get; private set; }
public sqliteMessageEventArg(sqliteMessageType type,string data,string Strsql)
{
EventId = type;
EventData = data;
sqlCommand = Strsql;
}
}
public enum sqliteMessageType
{
None = 0,Info = 1,Msg = 2,Error = 3,Errorsql = 4,ConnectBroken = 5,Others = 9
}
//=======================主界面================================
static void Main(string[] args)
{
bHelpersqlite _DB = new DbHelpersqlite();
_DB.sqliteMessageEvent += new EventHandler<sqliteMessageEventArg>(_DB_sqliteMessageEvent);
string _sql = "select Seq,cstid,D_Time,flag from w_CSTinfo";
DataTable _dt=_DB.SelectUse(_sql);
... ...
}
/// <summary>
/// DB執行對象返回事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _DB_sqliteMessageEvent(object sender,sqliteMessageEventArg e)
{
switch (e.EventId)
{
case sqliteMessageType.Msg:
MessageBox.Show(e.EventData);
break;
case sqliteMessageType.Error:
MessageBox.Show(e.EventData + ",sql: " + e.sqlCommand);
break;
case sqliteMessageType.Errorsql:
MessageBox.Show(e.EventData);
break;
default:
break;
}
}