public override DataTable GetSchema(string collectionName, string[] restrictionValues) { try { return(base.GetSchema(collectionName, restrictionValues)); } catch (Exception ex) { DAL.WriteDebugLog("[3]GetSchema({0})异常重试!{1},连接字符串 {2}", collectionName, ex.Message, ConnectionString, Database.ConnName); // 如果没有数据库,登录会失败,需要切换到系统数据库再试试 return(ProcessWithSystem(s => base.GetSchema(collectionName, restrictionValues)) as DataTable); //var dbname = DatabaseName; //if (dbname != SystemDatabaseName) DatabaseName = SystemDatabaseName; //try //{ // return base.GetSchema(collectionName, restrictionValues); //} //finally //{ // if (dbname != SystemDatabaseName) DatabaseName = dbname; //} } }
/// <summary>创建数据库</summary> protected override void CreateDatabase() { if (String.IsNullOrEmpty(FileName) || File.Exists(FileName)) { return; } DAL.WriteDebugLog("创建数据库:{0}", FileName); //FileSource.ReleaseFile(Assembly.GetExecutingAssembly(), "Database.mdb", FileName, true); var dbe = new DBEngineClass(); try { var db = dbe.CreateDatabase(FileName, dbLangChineseSimplified); if (db != null) { db.Close(); Marshal.ReleaseComObject(db); } } catch (Exception ex) { XTrace.WriteException(ex); } finally { Marshal.ReleaseComObject(dbe); } }
protected override void CreateDatabase() { //base.CreateDatabase(); if (String.IsNullOrEmpty(FileName) || File.Exists(FileName)) { return; } //The miminum you must specify: //Hashtable parameters = new Hashtable(); //parameters.Add("User", "SYSDBA"); //parameters.Add("Password", "masterkey"); //parameters.Add("Database", @"c:\database.fdb"); //FbConnection.CreateDatabase(parameters); DAL.WriteDebugLog("创建数据库:{0}", FileName); DbConnection conn = Database.Factory.CreateConnection(); MethodInfoX method = MethodInfoX.Create(conn.GetType(), "CreateDatabase", new Type[] { typeof(String) }); if (method == null) { return; } method.Invoke(null, Database.ConnectionString); }
public override DataTable GetSchema(string collectionName, string[] restrictionValues) { try { return(base.GetSchema(collectionName, restrictionValues)); } catch (Exception ex) { DAL.WriteDebugLog("GetSchema({0})异常重试!{1},连接字符串 {2}", collectionName, ex.Message, ConnectionString); String dbname = DatabaseName; if (dbname != SystemDatabaseName) { DatabaseName = SystemDatabaseName; } DataTable dt = null; try { dt = base.GetSchema(collectionName, restrictionValues); } finally { if (dbname != SystemDatabaseName) { DatabaseName = dbname; } } return(dt); } }
protected override void OnSetConnectionString(XDbConnectionStringBuilder builder) { base.OnSetConnectionString(builder); // 正常情况下INSERT, UPDATE和DELETE语句不返回数据。 当开启count-changes,以上语句返回一行含一个整数值的数据——该语句插入,修改或删除的行数。 if (!builder.ContainsKey("count_changes")) { builder["count_changes"] = "1"; } // 优化SQLite,如果原始字符串里面没有这些参数,就设置这些参数 if (!builder.ContainsKey("Pooling")) { builder["Pooling"] = "true"; } if (!builder.ContainsKey("Cache Size")) { builder["Cache Size"] = "50000"; } // 加大Page Size会导致磁盘IO大大加大,性能反而有所下降 //if (!builder.ContainsKey("Page Size")) builder["Page Size"] = "32768"; // 这两个设置可以让SQLite拥有数十倍的极限性能,但同时又加大了风险,如果系统遭遇突然断电,数据库会出错,而导致系统无法自动恢复 if (!builder.ContainsKey("Synchronous")) { builder["Synchronous"] = "Off"; } // Journal Mode的内存设置太激进了,容易出事,关闭 //if (!builder.ContainsKey("Journal Mode")) builder["Journal Mode"] = "Memory"; // 数据库中一种高效的日志算法,对于非内存数据库而言,磁盘I/O操作是数据库效率的一大瓶颈。 // 在相同的数据量下,采用WAL日志的数据库系统在事务提交时,磁盘写操作只有传统的回滚日志的一半左右,大大提高了数据库磁盘I/O操作的效率,从而提高了数据库的性能。 if (!builder.ContainsKey("Journal Mode")) { builder["Journal Mode"] = "WAL"; } // 绝大多数情况下,都是小型应用,发生数据损坏的几率微乎其微,而多出来的问题让人觉得很烦,所以还是采用内存设置 // 将来可以增加自动恢复数据的功能 //if (!builder.ContainsKey("Journal Mode")) builder["Journal Mode"] = "Memory"; // 自动清理数据 if (builder.ContainsKey("autoVacuum")) { AutoVacuum = builder["autoVacuum"].ToBoolean(); builder.Remove("autoVacuum"); } // 默认超时时间 if (!builder.ContainsKey("Default Timeout")) { builder["Default Timeout"] = 5 + ""; } DAL.WriteDebugLog(builder.ToString()); }
protected override void CreateDatabase() { if (String.IsNullOrEmpty(FileName) || File.Exists(FileName)) { return; } //FileSource.ReleaseFile(Assembly.GetExecutingAssembly(), "SqlCe.sdf", FileName, true); DAL.WriteDebugLog("创建数据库:{0}", FileName); var sce = SqlCeEngine.Create(ConnectionString); if (sce != null) { sce.CreateDatabase().Dispose(); } }
/// <summary>创建数据库</summary> protected virtual void CreateDatabase() { if (String.IsNullOrEmpty(FileName)) { return; } // 提前创建目录 String dir = Path.GetDirectoryName(FileName); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (!File.Exists(FileName)) { DAL.WriteDebugLog("创建数据库:{0}", FileName); File.Create(FileName).Dispose(); } }
/// <summary>根据数据行取得数据表</summary> /// <param name="rows">数据行</param> /// <returns></returns> protected List <IDataTable> GetTables(DataRow[] rows) { if (_columns == null) { try { _columns = GetSchema(_.Columns, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } if (_indexes == null) { try { _indexes = GetSchema(_.Indexes, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } if (_indexColumns == null) { try { _indexColumns = GetSchema(_.IndexColumns, null); } catch (Exception ex) { DAL.WriteDebugLog(ex.ToString()); } } try { var list = new List <IDataTable>(); foreach (DataRow dr in rows) { #region 基本属性 IDataTable table = DAL.CreateTable(); table.TableName = GetDataRowValue <String>(dr, _.TalbeName); // 顺序、编号 Int32 id = 0; if (TryGetDataRowValue <Int32>(dr, "TABLE_ID", out id)) { table.ID = id; } else { table.ID = list.Count + 1; } // 描述 table.Description = GetDataRowValue <String>(dr, "DESCRIPTION"); // 拥有者 table.Owner = GetDataRowValue <String>(dr, "OWNER"); // 是否视图 table.IsView = "View".EqualIgnoreCase(GetDataRowValue <String>(dr, "TABLE_TYPE")); table.DbType = Database.DbType; #endregion #region 字段及修正 // 字段的获取可能有异常,但不应该影响整体架构的获取 try { var columns = GetFields(table); if (columns != null && columns.Count > 0) { table.Columns.AddRange(columns); } var indexes = GetIndexes(table); if (indexes != null && indexes.Count > 0) { table.Indexes.AddRange(indexes); } // 先修正一次关系数据 table.Fix(); } catch (Exception ex) { if (DAL.Debug) { DAL.WriteLog(ex.ToString()); } } FixTable(table, dr); list.Add(table); #endregion } #region 表间关系处理 //// 某字段名,为另一个表的(表名+单主键名)形式时,作为关联字段处理 //foreach (var table in list) //{ // foreach (var rtable in list) // { // if (table != rtable) table.Connect(rtable); // } //} ModelHelper.Connect(list); //// 因为可能修改了表间关系,再修正一次 //foreach (var table in list) //{ // table.Fix(); //} #endregion return(list); // 不要把这些清空。因为,多线程同时操作的时候,前面的线程有可能把后面线程的数据给清空了 } finally { _columns = null; _indexes = null; _indexColumns = null; } }
/// <summary>获取索引</summary> /// <param name="table"></param> /// <returns></returns> protected virtual List <IDataIndex> GetIndexes(IDataTable table) { if (_indexes == null) { return(null); } DataRow[] drs = _indexes.Select(String.Format("{0}='{1}'", _.TalbeName, table.TableName)); if (drs == null || drs.Length < 1) { return(null); } List <IDataIndex> list = new List <IDataIndex>(); foreach (DataRow dr in drs) { String name = null; if (!TryGetDataRowValue <String>(dr, _.IndexName, out name)) { continue; } IDataIndex di = table.CreateIndex(); di.Name = name; if (TryGetDataRowValue <string>(dr, _.ColumnName, out name) && !String.IsNullOrEmpty(name)) { di.Columns = name.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); } else if (_indexColumns != null) { String orderby = null; // Oracle数据库用ColumnPosition,其它数据库用OrdinalPosition if (_indexColumns.Columns.Contains(_.OrdinalPosition)) { orderby = _.OrdinalPosition; } else if (_indexColumns.Columns.Contains(_.ColumnPosition)) { orderby = _.ColumnPosition; } DataRow[] dics = _indexColumns.Select(String.Format("{0}='{1}' And {2}='{3}'", _.TalbeName, table.TableName, _.IndexName, di.Name), orderby); if (dics != null && dics.Length > 0) { List <String> ns = new List <string>(); foreach (DataRow item in dics) { String dcname = null; if (TryGetDataRowValue <String>(item, _.ColumnName, out dcname) && !String.IsNullOrEmpty(dcname) && !ns.Contains(dcname)) { ns.Add(dcname); } } if (ns.Count < 1) { DAL.WriteDebugLog("表{0}的索引{1}无法取得字段列表!", table, di.Name); } di.Columns = ns.ToArray(); } } Boolean b = false; if (TryGetDataRowValue <Boolean>(dr, "UNIQUE", out b)) { di.Unique = b; } if (TryGetDataRowValue <Boolean>(dr, "PRIMARY", out b)) { di.PrimaryKey = b; } else if (TryGetDataRowValue <Boolean>(dr, "PRIMARY_KEY", out b)) { di.PrimaryKey = b; } FixIndex(di, dr); list.Add(di); } return(list != null && list.Count > 0 ? list : null); }