没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
C#中增加中增加SQLite事务操作支持与使用方法事务操作支持与使用方法
主要介绍了C#中增加SQLite事务操作支持与使用方法,结合实例形式分析了C#中针对SQLite事务操作的添加及使用技巧,需要的朋友
可以参考下
本文实例讲述了C#中增加SQLite事务操作支持与使用方法。分享给大家供大家参考,具体如下:
在C#中使用Sqlite增加对transaction支持
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
namespace Simple_Disk_Catalog
{
public class SQLiteDatabase
{
String DBConnection;
private readonly SQLiteTransaction _sqLiteTransaction;
private readonly SQLiteConnection _sqLiteConnection;
private readonly bool _transaction;
/// <summary>
/// Default Constructor for SQLiteDatabase Class.
/// </summary>
/// <param name="transaction">Allow programmers to insert, update and delete values in one transaction</param>
public SQLiteDatabase(bool transaction = false)
{
_transaction = transaction;
DBConnection = "Data Source=recipes.s3db";
if (transaction)
{
_sqLiteConnection = new SQLiteConnection(DBConnection);
_sqLiteConnection.Open();
_sqLiteTransaction = _sqLiteConnection.BeginTransaction();
}
}
/// <summary>
/// Single Param Constructor for specifying the DB file.
/// </summary>
/// <param name="inputFile">The File containing the DB</param>
public SQLiteDatabase(String inputFile)
{
DBConnection = String.Format("Data Source={0}", inputFile);
}
/// <summary>
/// Commit transaction to the database.
/// </summary>
public void CommitTransaction()
{
_sqLiteTransaction.Commit();
_sqLiteTransaction.Dispose();
_sqLiteConnection.Close();
_sqLiteConnection.Dispose();
}
/// <summary>
/// Single Param Constructor for specifying advanced connection options.
/// </summary>
/// <param name="connectionOpts">A dictionary containing all desired options and their values</param>
public SQLiteDatabase(Dictionary<String, String> connectionOpts)
{
String str = connectionOpts.Aggregate("", (current, row) => current + String.Format("{0}={1}; ", row.Key, row.Value));
str = str.Trim().Substring(0, str.Length - 1);
DBConnection = str;
}
/// <summary>
/// Allows the programmer to create new database file.
/// </summary>
/// <param name="filePath">Full path of a new database file.</param>
/// <returns>true or false to represent success or failure.</returns>
public static bool CreateDB(string filePath)
{
try
{
SQLiteConnection.CreateFile(filePath);
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
/// <summary>
/// Allows the programmer to run a query against the Database.
/// </summary>
/// <param name="sql">The SQL to run</param>
/// <param name="allowDBNullColumns">Allow null value for columns in this collection.</param>
/// <returns>A DataTable containing the result set.</returns>
public DataTable GetDataTable(string sql, IEnumerable<string> allowDBNullColumns = null)
{
var dt = new DataTable();
资源评论
weixin_38628953
- 粉丝: 6
- 资源: 927
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功