您的位置:首页 > 数据库

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/
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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: