C# SQLite基础工具类

目录

1、安装System.Data.SQLite工具包

2、创建数据库

3、数据库的连接与断开

4、执行一条SQL语句

5、批量执行sql语句

6、返回首行首列值

7、执行sql语句返回datatable


1、安装System.Data.SQLite工具包
2、创建数据库

/// <summary>

/// 数据库路径

/// </summary>

private string databasepath = Application.StartupPath + @"\DB\";

/// <summary>

/// 数据库名称

/// </summary>

private const string databasename = "AnDB.db";

/// <summary>

/// 创建数据库

/// </summary>

public void CreateDataBase()

{

try

{//判断数据库是否存在

if (!File.Exists(databasepath + databasename))

{

if (!Directory.Exists(databasepath))//判断文件夹是否存在不存在则创建

Directory.CreateDirectory(databasepath);

SQLiteConnection.CreateFile(databasepath + databasename);

}

}

catch

{//异常处理

}

}

3、数据库的连接与断开

public string SQLiteConnectiongString = "Data Source =" + Application.StartupPath + @"\DB\AnDB.db" + ";Pooling = true; FailIfMissing = true";//连接字符串

public SQLiteConnection Conn = null;

public bool StarConn()//连接

{

try

{

Conn = new SQLiteConnection(SQLiteConnectiongString);

if (Conn.State != ConnectionState.Open)//判断数据库是否打开

{

Conn.Open();//建立连接

}

return Conn.State ==ConnectionState.Open;

}

catch (Exception)

{//异常处理

return false;

}

}

public void CloseConn()// 解除connection

{

if (Conn.State == ConnectionState.Open || Conn.State == ConnectionState.Broken)

Conn.Close();

}

4、执行一条SQL语句

public bool ExecuteSQL(string strsql) //执行一条SQL语句,实现数据库事务。

{

//创建MySqlCommand执行命令语句对象

using (SQLiteCommand cmd = new SQLiteCommand())//使用using语句,方便using语句中声明的对象自动被Dispose

{

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

cmd.Connection = Conn; //MySqlCommand执行命令语句对象添加数据库连接对象

try

{

cmd.CommandType = CommandType.Text;

cmd.CommandText = strsql;

cmd.ExecuteNonQuery(); //执行非查询数据库操作

return true;

}

catch (Exception ex)

{//异常处理

return false;

}

}

}

5、批量执行sql语句

public bool BatchExecuteSQL(List<string> strsqllist)

{

if (strsqllist.Count == 0)

{

return false;

}

else

{ //创建MySqlCommand执行命令语句对象

using (SQLiteCommand cmd = new SQLiteCommand())//使用using语句,方便using语句中声明的对象自动被Dispose

{

if (Conn.State!=ConnectionState.Open) //判断是否有连接

{

Conn.Open();

}

SQLiteTransaction tx = Conn.BeginTransaction(IsolationLevel.ReadCommitted);//创建事务solationLevel.ReadCommitted

cmd.Transaction = tx; //MySqlCommand执行命令语句对象添加事务对象

cmd.Connection = Conn; //MySqlCommand执行命令语句对象添加数据库连接对象

try

{

for (int n = 0; n < strsqllist.Count; n++) //遍历SQL语句,依次执行

{

cmd.CommandType = CommandType.Text;

cmd.CommandText = strsqllist[n];

cmd.ExecuteNonQuery(); //执行非查询数据库操作

}

tx.Commit();//一次性提交事务

return true;

}

catch (Exception ex)

{

tx.Rollback();//异常处理

return false;

}

}

}

}

6、返回首行首列值

public int GetOnly(string strsql)//执行sql语句获取唯一值(总数,一列等)

{

try

{

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

using (SQLiteCommand cmd = new SQLiteCommand(strsql, Conn))

{

return Convert.ToInt32(cmd.ExecuteScalar());

}

}

catch (Exception ex)

{

return -1;

}

}

7、执行sql语句返回datatable

public DataTable GetDataTable(string strsql)//, params SQLiteParameter[] commandParameters) //sql查询 ,返回datatable

{

DataTable dt = new DataTable();

if (Conn.State!=ConnectionState.Open) //数据库连接

{

Conn.Open();

}

try //在这里用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在

{

using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(strsql, Conn))

{

adapter.Fill(dt); //将查询到数据填充到数据集

}

}

catch (Exception ex)

{//异常处理

}

return dt;

}

相关推荐
nongcunqq21 分钟前
abap 操作 excel
java·数据库·excel
rain bye bye1 小时前
calibre LVS 跑不起来 就将setup 的LVS Option connect下的 connect all nets by name 打开。
服务器·数据库·lvs
R-G-B1 小时前
【02】C#入门到精通——C# 变量、输入/输出、类型转换
开发语言·c#·c# 变量·c#输入/输出·c#类型转换
星河队长1 小时前
C# 软件加密方法,有使用时间限制,同时要防止拷贝
开发语言·c#
阿里云大数据AI技术2 小时前
云栖实录|MaxCompute全新升级:AI时代的原生数据仓库
大数据·数据库·云原生
Aevget3 小时前
DevExpress WinForms v25.1亮点 - PDF Viewer(查看器)等全新升级
pdf·c#·界面控件·winform·devexpress·ui开发
不剪发的Tony老师3 小时前
Valentina Studio:一款跨平台的数据库管理工具
数据库·sql
weixin_307779133 小时前
在 Microsoft Azure 上部署 ClickHouse 数据仓库:托管服务与自行部署的全面指南
开发语言·数据库·数据仓库·云计算·azure
InCerry3 小时前
为 .NET 10 GC(DATAS)做准备
性能优化·c#·.net·gc
六元七角八分3 小时前
pom.xml
xml·数据库