SQLiteHelper

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.IO;
None.gif
using System.Text.RegularExpressions;
None.gif
using System.Threading;
None.gif
using System.Data.SQLite;
None.gif
using System.Data;
None.gif
namespace Imps.Client.Pc
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
public class SQLiteHelper : IDisposable
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private SQLiteConnection _connection;
InBlock.gif        
private string _dataSource = string.Empty;
InBlock.gif        
private static SQLiteHelper _instance = new SQLiteHelper();
InBlock.gif        
private bool _isFirstUse;
InBlock.gif        
private Dictionary<int, SQLiteTransaction> _localTransactionCollection;
InBlock.gif        
private static object _locker = new object();
InBlock.gif        
private string _password = string.Empty;
InBlock.gif        
private static int _refCount = 0;
InBlock.gif        
private const string CONNECTION_STRING_FORMAT = "Data Source={0};Password={1}";
InBlock.gif        
private const string DATABASE_NAME = "FXRobot.dat";
InBlock.gif
InBlock.gif        
private SQLiteHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void CommitTransaction()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
InBlock.gif                
if (this.LocalTransactionCollection.ContainsKey(managedThreadId))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.LocalTransactionCollection[managedThreadId].Commit();
InBlock.gif                    _refCount
--;
InBlock.gif                    
this.LocalTransactionCollection.Remove(managedThreadId);
InBlock.gif                    
if (_refCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this._connection.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SQLiteCommand CreateCommand(string sql, params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SQLiteCommand command 
= null;
InBlock.gif            
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
InBlock.gif            
if (this.LocalTransactionCollection.ContainsKey(managedThreadId) && (this.LocalTransactionCollection[managedThreadId] != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                command 
= new SQLiteCommand(sql, this._connection, this.LocalTransactionCollection[managedThreadId]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                command 
= new SQLiteCommand(sql, this._connection);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (parameters != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (SQLiteParameter parameter in this.DeriveParameters(sql, parameters))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    command.Parameters.Add(parameter);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return command;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public List<SQLiteParameter> DeriveParameters(string commandText, object[] paramList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (paramList == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            List
<SQLiteParameter> list = new List<SQLiteParameter>();
InBlock.gif            
string input = commandText.Substring(commandText.IndexOf("@")).Replace(","" ,").Replace(")"" )");
InBlock.gif            
string pattern = @"(@)\S*(.*?)\b";
InBlock.gif            MatchCollection matchs 
= new Regex(pattern, RegexOptions.IgnoreCase).Matches(input);
InBlock.gif            List
<string> list2 = new List<string>();
InBlock.gif            
foreach (Match match in matchs)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!list2.Contains(match.Value))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    list2.Add(match.Value);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
string[] strArray = list2.ToArray();
InBlock.gif            
int index = 0;
InBlock.gif            Type type 
= null;
InBlock.gif            
foreach (object obj2 in paramList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (obj2 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    SQLiteParameter item 
= new SQLiteParameter();
InBlock.gif                    item.DbType 
= DbType.Object;
InBlock.gif                    item.ParameterName 
= strArray[index];
InBlock.gif                    item.Value 
= DBNull.Value;
InBlock.gif                    list.Add(item);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    type 
= obj2.GetType();
InBlock.gif                    SQLiteParameter parameter2 
= new SQLiteParameter();
InBlock.gif                    
switch (type.ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case "System.String":
InBlock.gif                            parameter2.DbType 
= DbType.String;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= (string)paramList[index];
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Byte[]":
InBlock.gif                            parameter2.DbType 
= DbType.Binary;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= (byte[])paramList[index];
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Int64":
InBlock.gif                            parameter2.DbType 
= DbType.Int64;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= (long)paramList[index];
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Int32":
InBlock.gif                            parameter2.DbType 
= DbType.Int32;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= (int)paramList[index];
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Boolean":
InBlock.gif                            parameter2.DbType 
= DbType.Boolean;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= (bool)paramList[index];
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.DateTime":
InBlock.gif                            parameter2.DbType 
= DbType.DateTime;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= Convert.ToDateTime(paramList[index]);
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Double":
InBlock.gif                            parameter2.DbType 
= DbType.Double;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= Convert.ToDouble(paramList[index]);
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Decimal":
InBlock.gif                            parameter2.DbType 
= DbType.Decimal;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= Convert.ToDecimal(paramList[index]);
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Guid":
InBlock.gif                            parameter2.DbType 
= DbType.Guid;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= (Guid)paramList[index];
InBlock.gif                            
goto Label_0408;
InBlock.gif
InBlock.gif                        
case "System.Object":
InBlock.gif                            parameter2.DbType 
= DbType.Object;
InBlock.gif                            parameter2.ParameterName 
= strArray[index];
InBlock.gif                            parameter2.Value 
= paramList[index];
InBlock.gif                            list.Add(parameter2);
InBlock.gif                            
goto Label_0408;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
throw new SystemException("Value is of unknown data type");
ExpandedSubBlockEnd.gif                }

InBlock.gif            Label_0408:
InBlock.gif                index
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return list;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Dispose(true);
InBlock.gif            GC.SuppressFinalize(
this);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Dispose(bool disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (this._localTransactionCollection != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
foreach (SQLiteTransaction transaction in this._localTransactionCollection.Values)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    transaction.Rollback();
InBlock.gif                                    transaction.Dispose();
InBlock.gif                                    
continue;
ExpandedSubBlockEnd.gif                                }

InBlock.gif                                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    
continue;
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
this._localTransactionCollection.Clear();
InBlock.gif                            
this._localTransactionCollection = null;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
if (this._connection != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this._connection.Close();
InBlock.gif                        
this._connection.Dispose();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._connection = null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void EnableConnection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this._connection == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string connectionString = string.Format("Data Source={0};Password={1}"this._dataSource, this._password);
InBlock.gif                
this._connection = new SQLiteConnection(connectionString);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this._connection.State == ConnectionState.Closed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this._connection.Open();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int ExecuteNonQuery(string sql, params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.EnableConnection();
InBlock.gif            
return this.CreateCommand(sql, parameters).ExecuteNonQuery();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SQLiteDataReader ExecuteReader(string sql, params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.EnableConnection();
InBlock.gif            
return this.CreateCommand(sql, parameters).ExecuteReader();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int ExecuteScalar(string sql, params object[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.EnableConnection();
InBlock.gif            
object obj2 = this.CreateCommand(sql, parameters).ExecuteScalar();
InBlock.gif            
if (obj2 != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return int.Parse(obj2.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
~SQLiteHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Dispose(false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool InitializeDatabase(string currentUserSid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool flag;
InBlock.gif            
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!this.Disposed)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Dispose();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
string app = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
InBlock.gif                app 
= Path.Combine(app, "Fetion");
InBlock.gif                
string path = Path.Combine(app, currentUserSid);
InBlock.gif                
if (!Directory.Exists(path))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Directory.CreateDirectory(path);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
this._dataSource = Path.Combine(path, DATABASE_NAME);
InBlock.gif                
this._password = currentUserSid;
InBlock.gif                
this._localTransactionCollection = new Dictionary<int, SQLiteTransaction>();
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (!File.Exists(this._dataSource))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        SQLiteConnection.CreateFile(
this._dataSource);
InBlock.gif                        
string connectionString = string.Format("Data Source={0};Password={1}"this._dataSource, this._password);
InBlock.gif                        
this._connection = new SQLiteConnection(connectionString);
InBlock.gif                        
this._connection.SetPassword(this._password);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    flag 
= true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Dispose();
InBlock.gif                    File.Delete(
this._dataSource);
InBlock.gif                    flag 
= false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void JoinTransaction()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.EnableConnection();
InBlock.gif                _refCount
++;
InBlock.gif                
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
InBlock.gif                
if (!this.LocalTransactionCollection.ContainsKey(managedThreadId))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.LocalTransactionCollection.Add(managedThreadId, this._connection.BeginTransaction());
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void RollbackTransaction()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
InBlock.gif                
if (this.LocalTransactionCollection.ContainsKey(managedThreadId))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.LocalTransactionCollection[managedThreadId].Rollback();
InBlock.gif                    _refCount
--;
InBlock.gif                    
this.LocalTransactionCollection.Remove(managedThreadId);
InBlock.gif                    
if (_refCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this._connection.Close();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool Disposed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (this._connection != null);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static SQLiteHelper Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _instance;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool IsFirstUse
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._isFirstUse;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Dictionary<int, SQLiteTransaction> LocalTransactionCollection
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (this._localTransactionCollection == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this._localTransactionCollection = new Dictionary<int, SQLiteTransaction>();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return this._localTransactionCollection;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public List<string> Objects
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock (_locker)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    List
<string> list = new List<string>();
InBlock.gif                    
using (SQLiteDataReader reader = this.ExecuteReader("SELECT [Name] FROM [SQLITE_MASTER] WHERE ([type] = 'table') OR ([type] = 'view')"null))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            list.Add(reader[
"name"].ToString());
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
return list;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/solo/archive/2008/07/10/1240138.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值