namespace QQLite.GuessFeng
{
/// <summary>
/// 说明:这是一个针对System.Data.SQLite的数据库常规操作封装的通用类。
/// 更新时间:2014-10-31
/// </summary>
public class SQLiteDBHelper//SQLiteDBHelper
{
public static string DataSource = string.Format("Data Source={0};Pooling=true;FailIfMissing=false", Path.Combine(D:\\, "GuessFeng.db"));
public static string dbpath = D:\\ + "GuessFeng.db";
private string connectionString = string.Empty;
#region 数据库连接必要条件参数
private SQLiteConnection dbConnection = null;
/// <summary>
/// 数据库连接
/// </summary>
public SQLiteConnection DbConnection
{
get
{
if (this.dbConnection == null)
{
// 若没打开,就变成自动打开关闭的
this.Open();
this.AutoOpenClose = true;
}
return this.dbConnection;
}
set
{
this.dbConnection = value;
}
}
private SQLiteCommand dbCommand = null;
/// <summary>
/// 命令
/// </summary>
public SQLiteCommand DbCommand
{
get
{
return this.dbCommand;
}
set
{
this.dbCommand = value;
}
}
private SQLiteDataAdapter dbDataAdapter = null;
/// <summary>
/// 数据库适配器
/// </summary>
public SQLiteDataAdapter DbDataAdapter
{
get
{
return this.dbDataAdapter;
}
set
{
this.dbDataAdapter = value;
}
}
/// <summary>
/// 数据库连接
/// </summary>
public string ConnectionString
{
get
{
return this.connectionString;
}
set
{
this.connectionString = value;
}
}
private SQLiteTransaction dbTransaction = null;
private bool inTransaction = false;
/// <summary>
/// 是否已采用事务
/// </summary>
public bool InTransaction
{
get
{
return this.inTransaction;
}
set
{
this.inTransaction = value;
}
}
private bool autoOpenClose = false;
/// <summary>
/// 默认打开关闭数据库选项(默认为否)
/// </summary>
public bool AutoOpenClose
{
get
{
return autoOpenClose;
}
set
{
autoOpenClose = value;
}
}
#endregion
/// <summary>
/// 构造函数
/// </summary>
/// <param name="dbPath">SQLite数据库文件路径</param>
public SQLiteDBHelper()
{
// string dbPath = Client.BasePath + @"QQ\" + Client.QQ + @"\DataBase\QQLite.Plugin.VipwarrantPlugin.db";
this.connectionString = DataSource;
}
/// <summary>
/// 这时主要的获取数据库连接的方法
/// </summary>
/// <returns>数据库连接</returns>
public IDbConnection Open()
{
this.Open(this.ConnectionString);
return this.dbConnection;
}
/// <summary>
/// 获得新的数据库连接
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <returns>数据库连接</returns>
public IDbConnection Open(string connectionString)
{
// 若是空的话才打开
if (this.dbConnection == null || this.dbConnection.State == ConnectionState.Closed)
{
this.ConnectionString = connectionString;
this.dbConnection = new SQLiteConnection(this.ConnectionString);
this.dbConnection.Open();
}
this.AutoOpenClose = false;
return this.dbConnection;
}
#region 建立本地数据库
/// <summary>
/// 创建SQLite数据库文件
/// </summary>
/// <param name="dbPath">要创建的SQLite数据库文件路径</param>
public void CreateDB()
{
if (!File.Exists(dbpath))
{
// 自动打开
if (this.DbConnection == null)
{
this.AutoOpenClose = true;
this.Open();
}
else if (this.DbConnection.State == ConnectionState.Closed)
{
this.Open();
}
this.dbCommand = this.DbConnection.CreateCommand();
this.dbCommand.CommandText =
"CREATE TABLE [DBVersion] ([Version] NVARCHAR(50) NULL);INSERT INTO [DBVersion] (Version) values('4.0.0.0');CREATE TABLE [RanNum] ([Id] INTEGER PRIMARY KEY NOT NULL,[ExternalId] INTEGER NOT NULL,[Num] INTEGER NOT NULL,[LastQQ]INTEGER NULL);CREATE TABLE [Config] ([Id] INTEGER PRIMARY KEY NOT NULL,[Fix] TEXT NULL,[Open] INTEGER NOT NULL,[Deduct] INTEGER NOT NULL,[Award] INTEGER NOT NULL);INSERT INTO [Config] (Fix,Open,Deduct,Award) values('#',10,5,100);CREATE TABLE [Lock] ([Id] INTEGER PRIMARY KEY NOT NULL,[value] TEXT NULL);";
this.dbCommand.ExecuteNonQuery();
//this.dbCommand.CommandText = "DROP TABLE Demo";
//this.dbCommand.ExecuteNonQuery();
}
}
#endregion
/// <summary>
/// 对SQLite数据库执行增删改操作,返回受影响的行数。
/// </summary>
/// <param name="commandText">要执行的增删改的SQL语句</param>
/// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>
/// <returns></returns>
public int ExecuteNonQuery(string commandText, SQLiteParameter[] parameters)
{
// 自动打开
if (this.DbConnection == null)
{
this.AutoOpenClose = true;
this.Open();
}
else if (this.DbConnection.State == ConnectionState.Closed)
{
this.Open();
}
this.dbCommand = this.DbConnection.CreateCommand();
this.dbCommand.CommandText = commandText;
if (this.dbTransaction != null)
{
this.dbCommand.Transaction = this.dbTransaction;
}
if (parameters != null)
{
this.dbCommand.Parameters.Clear();
for (int i = 0; i < parameters.Length; i++)
{
this.dbCommand.Parameters.Add(parameters[i]);
}
}
int returnValue = this.dbCommand.ExecuteNonQuery();
// 自动关闭
this.dbCommand.Parameters.Clear();
// 自动关闭
//if (this.AutoOpenClose)
//{
// this.Close();
//}
return returnValue;
}
#endregion
#region public void Close() 关闭数据库连接
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (this.dbConnection != null)
{
this.dbConnection.Close();
this.dbConnection.Dispose();
}
this.Dispose();
}
#endregion
#region public void Dispose() 内存回收
/// <summary>
/// 内存回收
/// </summary>
public void Dispose()
{
if (this.dbCommand != null)
{
this.dbCommand.Dispose();
}
if (this.dbDataAdapter != null)
{
this.dbDataAdapter.Dispose();
}
this.dbConnection = null;
}
#endregion
}
}
private void button1_Click(object sender, EventArgs e)
{
SQLiteDBHelper vdb = new SQLiteDBHelper();//初始化SQLite数据库
string sql = "UPDATE Config SET Fix=@Fix,Open=@Open,Deduct=@Deduct,Award=@Award WHERE (Id=1)";//修改语句
SQLiteParameter[] Config = new SQLiteParameter[]{
new SQLiteParameter("@Fix",text_Fix.Text ),
new SQLiteParameter ("@Open",text_Open.Text ),
new SQLiteParameter("@Deduct",text_Deduct.Text ),
new SQLiteParameter("@Award",text_Award.Text )
};
//MessageBox.Show("保存成功!");
if (vdb.ExecuteNonQuery(sql, Config) > 0)
{
MessageBox.Show("保存成功!");
}
}
为什么点击按钮保存,一点就卡死