目录
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;
}