直接上代码不废话
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sj.Browser.SQLite
{
public class SqliteDatabase
{
private SQLiteConnection m_conn = null;
private SQLiteCommand m_cmd = null;
protected string m_dbName;
protected string m_tablename;
protected string m_password;
/// <summary>
/// 实例化对象,并指定数据库名称
/// </summary>
/// <param name="dbName">数据库名称</param>
public SqliteDatabase(string dbName)
{
m_dbName = dbName;
m_tablename = "SourceInfo";
m_password = "123";
}
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
if (m_conn == null)
m_conn = new SQLiteConnection();
m_cmd = new SQLiteCommand();
m_cmd.Connection = m_conn;
}
/// <summary>
/// 创建数据库
/// </summary>
public void CreateDataBase()
{
//Create Database
SQLiteConnection.CreateFile(m_dbName);
Init();
ConnectDataBase();
}
/// <summary>
/// 打开数据库
/// </summary>
public void Open()
{
m_conn = new SQLiteConnection("Data Source=" + m_dbName +
";Version=3;New=False;Compress=True;");
Init();
ConnectDataBase();
}
/// <summary>
/// 设置数据库密码
/// </summary>
/// <param name="password"></param>
public void SetPassWord(string password)
{
m_password = password;
}
/// <summary>
/// 链接数据库
/// </summary>
public void ConnectDataBase()
{
//Connect to DataBase
SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
connstr.DataSource = m_dbName;
if (m_password != "")
connstr.Password = m_password;
m_conn.ConnectionString = connstr.ToString();
}
/// <summary>
/// 创建数据库表
/// </summary>
/// <param name="tableName">数据库表名</param>
public void CreateTable(string tableName)
{
m_tablename = tableName;
m_conn.Open();
string sql = "CREATE TABLE IF NOT EXISTS " + tableName + "(url nvarchar(50)
not null ,ver nvarchar(50),sourcefile blob(200))";
m_cmd.CommandText = sql;
m_cmd.ExecuteNonQuery();
m_conn.Close();
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="parameterss">url=@url,ver=@ver,sourcefile=@sourcefile</param>
public void InsertDate(params SQLiteParameter[] parameterss)
{
//Insert Data
m_conn.Open();
StringBuilder sql = new StringBuilder("insert into [" + m_tablename + "]
values(url=@url,ver=@ver,sourcefile=@sourcefile)");
m_cmd.CommandText = sql.ToString();
m_cmd.Parameters.AddRange(parameterss);
m_cmd.ExecuteNonQuery();
m_conn.Close();
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="sql"></param>
public void ExecuteSql(string sql)
{
using (m_conn = new SQLiteConnection(sql))
{
Open();
m_cmd = m_conn.CreateCommand();
m_cmd.CommandText = sql;
m_cmd.CommandType = CommandType.Text;
m_cmd.ExecuteNonQuery();
m_conn.Close();
}
}
/// <summary>
/// 执行查询返回DataSet
/// </summary>
/// <param name="sqlStr"></param>
/// <returns></returns>
public DataSet ExecuteDataSet(string sqlStr)
{
Open();
m_cmd.CommandText = sqlStr;
m_cmd.CommandType = CommandType.Text;
SQLiteDataAdapter da = new SQLiteDataAdapter(m_cmd);
DataSet ds = new DataSet();
da.Fill(ds);
m_conn.Close();
return ds;
}
}
}