C# 封装 System.Data.SQLite
2015-02-12 15:05
316 查看
参考1:
关于如何使用System.Data.SQLite的入门:
http://www.dreamincode.net/forums/topic/157830-using-sqlite-with-c%23/
View Code
It only uses one connection, one transaction and one command, so all you're changing is the parameter values each time.
我自己实现的一个简易版本:
关于如何使用System.Data.SQLite的入门:
http://www.dreamincode.net/forums/topic/157830-using-sqlite-with-c%23/
public void InsertItems() { SQLiteConnection connection = new SQLiteConnection(SomeConnectionString); SQLiteCommand command = connection.CreateCommand(); SQLiteTransaction transaction = connection.BeginTransaction(); command.CommandText = "INSERT OR IGNORE INTO Result " + "(RunTag, TopicId, DocumentNumber, Rank, Score) " + "VALUES (@RunTag, @TopicId, @DocumentNumber, @Rank, @Score)"; command.Parameters.AddWithValue("@RunTag", ""); command.Parameters.AddWithValue("@TopicId", ""); command.Parameters.AddWithValue("@DocumentNumber", ""); command.Parameters.AddWithValue("@Rank", ""); command.Parameters.AddWithValue("@Score", ""); foreach ( /* item to loop through and add to db */ ) { InsertResultItem(runTag, topicId, documentNumber, rank, score, command); } transaction.Commit(); command.Dispose(); connection.Dispose(); } public int InsertResultItem(string runTag, int topicId, string documentNumber, int rank, double score, SQLiteCommand command) { command.Parameters["@RunTag"].Value = runTag; command.Parameters["@TopicId"].Value = topicId; command.Parameters["@DocumentNumber"].Value = documentNumber; command.Parameters["@Rank"].Value = rank; command.Parameters["@Score"].Value = score; return command.ExecuteNonQuery(); }
View Code
It only uses one connection, one transaction and one command, so all you're changing is the parameter values each time.
我自己实现的一个简易版本:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SQLite; namespace Utility { /*Multithread safety: No!*/ public class SQLiteDB { private SQLiteConnection m_db_conn; private SQLiteTransaction m_transaction; private bool m_is_transaction; private SQLiteCommand m_transaction_cmd; public SQLiteDB() { m_is_transaction = false; } public SQLiteDB(string db_path) { m_is_transaction = false; m_db_conn = new SQLiteConnection(string.Format("Data Source={0}", db_path)); m_db_conn.Open(); } public void Open(string db_path) { m_db_conn = new SQLiteConnection(string.Format("Data Source={0}", db_path)); m_db_conn.Open(); } public void Close() { if (m_db_conn.State != System.Data.ConnectionState.Closed) { m_db_conn.Close(); m_db_conn.Dispose(); } if (null != m_transaction_cmd) { m_transaction_cmd.Dispose(); } } public void BeginTransaction() { m_is_transaction = true; m_transaction_cmd = m_db_conn.CreateCommand(); m_transaction = m_db_conn.BeginTransaction(); } public void Commit() { if (m_is_transaction) { m_transaction.Commit(); m_transaction_cmd.Dispose(); m_is_transaction = false; } } public void Rollback() { if (m_is_transaction) { m_transaction.Rollback(); m_transaction_cmd.Dispose(); m_is_transaction = false; } } public int ExecuteNonQuery(string non_query_sql) { if (!m_is_transaction) { SQLiteCommand sql_cmd = new SQLiteCommand(m_db_conn); sql_cmd.CommandText = non_query_sql; return sql_cmd.ExecuteNonQuery(); } else { m_transaction_cmd.CommandText = non_query_sql; return m_transaction_cmd.ExecuteNonQuery(); } } } }
相关文章推荐
- C# Winform 未能加载文件或程序集"System.Data.SQLite"或它的某一个依赖项。试图加载格式不正确的程序
- C#引用System.Data.SQLite操作SQLite数据库一例
- C#2010 在使用 System.Data.SQLite.dll 时出现异常
- C#:System.Data.SQLite数据库介绍
- C# 调用System.Data.Sqlite 外键失效解决方案
- C# System.Data.Sqlite.dll 的注意事项
- C#写的64位windows窗口应用程序打包安装后提示“未能加载文件或程序集‘System.Data.SQLite’”解决方法
- C#解决类似System.Data.SQLite.SQLiteParameter' is defined in an assembly that is not referenced. You must
- VS2015 C# System.Data.SQLite
- C#使用System.Data.SQLite操作SQLite
- C#使用System.Data.SQLite操作SQLite
- 封装了 System.Data.SQLite 的数据库助手类
- C# system.data.sqlite使用
- C# System.Data.Sqlite.dll 的注意事项
- System.Data.SQLite数据库介绍
- System.Data.SQLite数据库
- System.Data.SQLite数据库简介
- SQLite.Interop.DLL与System.Data.SQLite.dll比较
- System.Data.SQLite 1.0.66.0不能在 .net4.0下运行
- System.Data.SQLite数据库简介