Esempio n. 1
0
        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="databaseFilePath"></param>
        /// <param name="createTableSql"></param>
        public void CreateDb(string databaseFilePath, string createTableSql)
        {
            if (File.Exists(databaseFilePath))
            {
                Logger.WriteInfoFmt(Log.CreateDb, "检查路径{0}确认sqllite文件存在", databaseFilePath);
            }
            else
            {
                Logger.WriteWarningFmt(Log.CreateDb, "检查路径{0}确认sqllite文件不存在,开始建立sqlite", databaseFilePath);
                File.Create(databaseFilePath).Close();
                SetFileSystemAccessRule(databaseFilePath);
                Logger.WriteInfoFmt(Log.CreateDb, "在{0}创建sqllite文件结束", databaseFilePath);

                if (!string.IsNullOrEmpty(createTableSql))
                {
                    try
                    {
                        string        connectionString = string.Concat("Data Source=", databaseFilePath);
                        SqliteContext sqlite           = new SqliteContext(new DBContextBridge
                        {
                            ConnectionString = connectionString,
                            Provider         = "sqlite",
                        });
                        sqlite.ExecuteSQL("", createTableSql);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteErrorFmt(Log.CreateDb, "创建数据表出现异常,异常信息:{0}", ex);
                    }
                }
            }
        }
        /// <summary>
        /// 根据类型创建表
        /// </summary>
        /// <param name="type"></param>
        private void CreateTable(Type type)
        {
            SqliteContext sqliteContext = new SqliteContext(new DBContextBridge
            {
                ConnectionString = _connStr,
                Provider         = "sqlite",
            });

            sqliteContext.ExecuteSQL("", Class2DbHelper.Instance[type].GetCreateTableSql());
        }
 /// <summary>
 /// 写数据
 /// </summary>
 /// <param name="queueGroup"></param>
 private void WriteData(QueueGroup queueGroup)
 {
     try
     {
         foreach (var key in queueGroup.QueueDic.Keys)
         {//TODO:根据类型获取表名
             if (!_tableList.Contains(Class2DbHelper.Instance[key].ClassAttribute.TableName))
             {
                 //创建表
                 CreateTable(key);
                 _tableList.Add(Class2DbHelper.Instance[key].ClassAttribute.TableName);
             }
             //插入数据
             int queueCurrentCount = queueGroup.QueueDic[key].Count;
             if (queueCurrentCount > 0)
             {
                 SqliteContext sqliteContext = new SqliteContext(new DBContextBridge
                 {
                     ConnectionString = _connStr,
                     Provider         = "sqlite",
                 });
                 int transid = 0;
                 for (int i = 0; i < queueCurrentCount; i++)
                 {
                     if (transid == 0)
                     {
                         transid = sqliteContext.RegisteTrans();
                     }
                     var entity = queueGroup.QueueDic[key].Dequeue() as SqliteEntity;
                     if (entity != null)
                     {
                         //string opid = entity.GetValue(entity, "ContentOpid").ToString();
                         //string domainId = entity.GetValue(entity, "DomainId").ToString();
                         //string createTime = entity.GetValue(entity, "CreateTime").ToString();
                         //Logger.WriteError("输出GetValue", string.Format("ContentOpid:{0},DomainId:{1},CreateTime:{2}", opid, domainId, createTime));
                         sqliteContext.ExecuteSQL(transid, Class2DbHelper.Instance[key].GetInsertSqliteSql(entity));
                         if ((i > 0 && i % 10000 == 0) || i == queueCurrentCount - 1)
                         {
                             sqliteContext.TransCommit(transid);
                             transid = 0;
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         throw new ApplicationException("写sqlite数据时出现异常", ex);
     }
 }