//连接字符串
conn = @"Data Source=E:\sqlite.db";
string sql_table = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '%prod%'";
DataTable dt_tbname = SQLiteHelper.Query(conn, sql_table).Tables[0];
//工具类SQLiteHelper
查看SQLiteHelper
using System;
using System.Collections;
using System.Data;
using System.Data.SQLite;
using System.Threading;
//using Topshelf.Logging;
public class SQLiteHelper
{
//private static readonly LogWriter logger = HostLogger.Get<SQLiteHelper>();
//public static string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\data\\data.db";
public static string connectionString = @"Data Source=E:\XXX.db";
private static object ExecuteNonQuery_locker = new object();
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString,out string sqlerr_msg)
{
sqlerr_msg = "";
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
int row = cmd.ExecuteNonQuery();
return row;
}
catch (System.Data.SQLite.SQLiteException E)
{
connection.Close();
sqlerr_msg = E.Message;
//logger.Error("插入數據異常Service-insert_path_sqlite " + E.Message);
//throw new Exception(E.Message);
}
}
}
return 0;
}
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
int row = cmd.ExecuteNonQuery();
return row;
}
catch (System.Data.SQLite.SQLiteException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
return 0;
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">多条SQL语句</param>
public static void ExecuteSqlTran(ArrayList SQLStringList)
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
SQLiteTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.SQLite.SQLiteException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(String conn, string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(conn))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}