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;

}

相关推荐
遗憾皆是温柔8 分钟前
MyBatis—动态 SQL
java·数据库·ide·sql·mybatis
想做后端的小C16 分钟前
C# 面向对象 构造函数带参无参细节解析
开发语言·c#·面向对象
炯哈哈29 分钟前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机
未来之窗软件服务29 分钟前
Cacti 未经身份验证SQL注入漏洞
android·数据库·sql·服务器安全
bestcxx31 分钟前
c# UTC 时间赋值注意事项
c#·utc
酷炫码神34 分钟前
C#运算符
开发语言·c#
fengye2071611 小时前
在MYSQL中导入cookbook.sql文件
数据库·mysql·adb
Ailovelearning1 小时前
neo4j框架:ubuntu系统中neo4j安装与使用教程
数据库·neo4j
zybsjn1 小时前
后端系统做国际化改造,生成多语言包
java·python·c#
敲代码的 蜡笔小新2 小时前
【行为型之迭代器模式】游戏开发实战——Unity高效集合遍历与场景管理的架构精髓
unity·设计模式·c#·迭代器模式