252 lines
7.5 KiB
C#
252 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
using System.Data.SQLite;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Data.Common;
|
|
|
|
namespace AllBase
|
|
{
|
|
public class SqliteHelper
|
|
{
|
|
//获取连接字符串
|
|
|
|
private static readonly string str = "data source =DB.dll"; //ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
|
|
|
|
public static void CreateProductTable()
|
|
{
|
|
DBConfig.DBName = "DB.dll";
|
|
DBConfig.CreateDbFile();
|
|
DBConfig.CreateProductTable();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 做增删改的功能
|
|
/// </summary>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <param name="ps">sql语句中的参数</param>
|
|
/// <returns>受影响的行数</returns>
|
|
public static int ExecuteNonQuery(string sql, params SQLiteParameter[] ps)
|
|
{
|
|
try
|
|
{
|
|
//连接数据库
|
|
using (SQLiteConnection con = new SQLiteConnection(str))
|
|
{
|
|
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
|
|
{
|
|
con.Open();//打开数据库
|
|
if (ps != null)
|
|
{
|
|
cmd.Parameters.AddRange(ps);//参数
|
|
}
|
|
return cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public static int ExecuteNonQueryInsert(string sql, SQLiteCommand cmd)
|
|
{
|
|
try
|
|
{
|
|
cmd.CommandText = sql;
|
|
return cmd.ExecuteNonQuery();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 做增删改的功能
|
|
/// </summary>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <param name="ps">sql语句中的参数</param>
|
|
/// <returns>受影响的行数</returns>
|
|
public static int BatchExecuteNonQuery(List<string> lst)
|
|
{
|
|
try
|
|
{
|
|
using (SQLiteConnection con = new SQLiteConnection(str))
|
|
{
|
|
using (SQLiteCommand cmd = new SQLiteCommand())
|
|
{
|
|
con.Open();
|
|
cmd.Connection = con;
|
|
using (var transaction = con.BeginTransaction())
|
|
{
|
|
for (int i = 0; i < lst.Count; i++)
|
|
{
|
|
ExecuteNonQueryInsert(lst[i],cmd);
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
con.Close();
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
return 0;
|
|
}
|
|
/// <summary>
|
|
/// 去掉SQL中的特殊字符
|
|
/// </summary>
|
|
/// <param name="value">字符串</param>
|
|
/// <returns></returns>
|
|
public static string ReplaceSqlx(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
return null;
|
|
value = value.Replace("\\", "\\\\");
|
|
value = value.Replace("'", "''");
|
|
//value = value.Replace("\"", "\\\"");
|
|
return value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 首行首列
|
|
/// </summary>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <param name="ps">sql语句中的参数</param>
|
|
/// <returns>返回的是首行首列</returns>
|
|
public static object ExecuteScalar(string sql, params SQLiteParameter[] ps)
|
|
{
|
|
using (SQLiteConnection con = new SQLiteConnection(str))
|
|
{
|
|
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
|
|
{
|
|
con.Open();
|
|
if (ps != null)
|
|
{
|
|
cmd.Parameters.AddRange(ps);
|
|
}
|
|
return cmd.ExecuteScalar();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 查询的
|
|
/// </summary>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <param name="ps">sql语句中的参数</param>
|
|
/// <returns>返回的是SqliteDataReader</returns>
|
|
public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] ps)
|
|
{
|
|
SQLiteConnection con = new SQLiteConnection(str);
|
|
using (SQLiteCommand cmd = new SQLiteCommand(sql, con))
|
|
{
|
|
if (ps != null)
|
|
{
|
|
cmd.Parameters.AddRange(ps);
|
|
}
|
|
try
|
|
{
|
|
con.Open();
|
|
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
con.Close();
|
|
con.Dispose();
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 查询数据
|
|
/// </summary>
|
|
/// <param name="sql">sql语句</param>
|
|
/// <param name="ps">sql语句中的参数</param>
|
|
/// <returns>返回表</returns>
|
|
public static DataTable ExecuteTable(string sql, params SQLiteParameter[] ps)
|
|
{
|
|
try
|
|
{
|
|
DataTable dt = new DataTable();
|
|
using (SQLiteDataAdapter sda=new SQLiteDataAdapter(sql,str))
|
|
{
|
|
if (ps!=null)
|
|
{
|
|
sda.SelectCommand.Parameters.AddRange(ps);
|
|
}
|
|
sda.Fill(dt);
|
|
return dt;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
return new DataTable();
|
|
}
|
|
}
|
|
|
|
public class DBConfig
|
|
{
|
|
public static string DBName = "DB.dll"; //数据库的名字
|
|
|
|
public static string DataSource
|
|
{
|
|
get
|
|
{
|
|
string databaseFile = GetDatabaseFile();
|
|
return string.Format("data source={0}", databaseFile);
|
|
}
|
|
}
|
|
public static void CreateDirectory(string dir)
|
|
{
|
|
if (!Directory.Exists(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
}
|
|
|
|
public static bool CreateDbFile()
|
|
{
|
|
string databaseFile = GetDatabaseFile();
|
|
try
|
|
{
|
|
CreateDirectory(Path.GetDirectoryName(databaseFile));
|
|
if (!File.Exists(databaseFile))
|
|
{
|
|
SQLiteConnection.CreateFile(databaseFile);
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("新建数据库文件" + databaseFile + "失败:" + ex.Message);
|
|
}
|
|
}
|
|
|
|
private static string GetDatabaseFile()
|
|
{
|
|
string databaseFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,DBName);
|
|
return databaseFile;
|
|
}
|
|
|
|
public static void CreateProductTable()
|
|
{
|
|
string sql = "CREATE TABLE if not exists Product (content STRING, fromid STRING PRIMARY KEY);";
|
|
try
|
|
{
|
|
SqliteHelper.ExecuteNonQuery(sql);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|