Example #1
0
        protected override void OnSetConnectionString(ConnectionStringBuilder builder)
        {
            base.OnSetConnectionString(builder);

            SqlCeVer = SQLCEVersion.SQLCE40;

            var fn = DatabaseName;

            if (!fn.IsNullOrEmpty() && File.Exists(fn))
            {
                try
                {
                    SqlCeVer = SqlCeHelper.DetermineVersion(fn);
                }
                catch (Exception ex)
                {
                    XTrace.WriteException(ex);

                    SqlCeVer = SQLCEVersion.SQLCE40;
                }
            }
        }
Example #2
0
File: DB2.cs Project: pjy612/X
        protected override void OnSetConnectionString(ConnectionStringBuilder builder)
        {
            base.OnSetConnectionString(builder);

            // 修正数据源
            if (builder.TryGetAndRemove("Data Source", out var str) && !str.IsNullOrEmpty())
            {
                if (str.Contains("://"))
                {
                    var uri  = new Uri(str);
                    var type = uri.Scheme.IsNullOrEmpty() ? "TCP" : uri.Scheme.ToUpper();
                    var port = uri.Port > 0 ? uri.Port : 1521;
                    var name = uri.PathAndQuery.TrimStart("/");
                    if (name.IsNullOrEmpty())
                    {
                        name = "ORCL";
                    }

                    str = $"(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL={type})(HOST={uri.Host})(PORT={port})))(CONNECT_DATA=(SERVICE_NAME={name})))";
                }
                builder.TryAdd("Data Source", str);
            }
        }
Example #3
0
 /// <summary>设置连接字符串时允许从中取值或修改,基类用于读取拥有者Owner,子类重写时应调用基类</summary>
 /// <param name="builder"></param>
 protected virtual void OnSetConnectionString(ConnectionStringBuilder builder)
 {
     if (builder.TryGetAndRemove(nameof(Owner), out var value) && !value.IsNullOrEmpty())
     {
         Owner = value;
     }
     if (builder.TryGetAndRemove(nameof(ShowSQL), out value) && !value.IsNullOrEmpty())
     {
         ShowSQL = value.ToBoolean();
     }
     if (builder.TryGetAndRemove(nameof(UserParameter), out value) && !value.IsNullOrEmpty())
     {
         UserParameter = value.ToBoolean();
     }
     if (builder.TryGetAndRemove(nameof(Migration), out value) && !value.IsNullOrEmpty())
     {
         Migration = (Migration)Enum.Parse(typeof(Migration), value, true);
     }
     if (builder.TryGetAndRemove(nameof(TablePrefix), out value) && !value.IsNullOrEmpty())
     {
         TablePrefix = value;
     }
 }
Example #4
0
        protected override void OnSetConnectionString(ConnectionStringBuilder builder)
        {
            base.OnSetConnectionString(builder);

            var key = builder[Server_Key];

            if (key.EqualIgnoreCase(".", "localhost"))
            {
                //builder[Server_Key] = "127.0.0.1";
                builder[Server_Key] = IPAddress.Loopback.ToString();
            }

            // 默认设置为utf8mb4,支持表情符
            builder.TryAdd(CharSet, "utf8mb4");

            //if (!builder.ContainsKey(AllowZeroDatetime)) builder[AllowZeroDatetime] = "True";
            // 默认最大连接数1000
            if (builder["Pooling"].ToBoolean())
            {
                builder.TryAdd(MaxPoolSize, "1000");
            }

            // 如未设置Sslmode,默认为none
            if (builder[Sslmode] == null)
            {
                builder.TryAdd(Sslmode, "none");
            }

            // 如果是新版驱动v8.0,需要设置获取公钥
            var version = Factory?.GetType().Assembly.GetName().Version;

            if (version != null && version.Major >= 8)
            {
                builder.TryAdd("AllowPublicKeyRetrieval", "true");
            }
        }
Example #5
0
        private static void OpenDatabase(IDbConnection conn, String dbName)
        {
            // 如果没有打开,则改变链接字符串
            var builder = new ConnectionStringBuilder(conn.ConnectionString);
            var flag    = false;

            if (builder["Database"] != null)
            {
                builder["Database"] = dbName;
                flag = true;
            }
            else if (builder["Initial Catalog"] != null)
            {
                builder["Initial Catalog"] = dbName;
                flag = true;
            }
            if (flag)
            {
                var connStr = builder.ToString();
                conn.ConnectionString = connStr;
            }

            conn.Open();
        }
Example #6
0
        protected override void OnSetConnectionString(ConnectionStringBuilder builder)
        {
            base.OnSetConnectionString(builder);

            var flag = Factory != null && Factory.GetType().FullName.StartsWith("System.Data");

            if (flag)
            {
                //// 正常情况下INSERT, UPDATE和DELETE语句不返回数据。 当开启count-changes,以上语句返回一行含一个整数值的数据——该语句插入,修改或删除的行数。
                //if (!builder.ContainsKey("count_changes")) builder["count_changes"] = "1";

                // 优化SQLite,如果原始字符串里面没有这些参数,就设置这些参数
                //builder.TryAdd("Pooling", "true");
                //if (!builder.ContainsKey("Cache Size")) builder["Cache Size"] = "5000";
                builder.TryAdd("Cache Size", (512 * 1024 * 1024 / -1024) + "");
                // 加大Page Size会导致磁盘IO大大加大,性能反而有所下降
                //if (!builder.ContainsKey("Page Size")) builder["Page Size"] = "32768";
                // 这两个设置可以让SQLite拥有数十倍的极限性能,但同时又加大了风险,如果系统遭遇突然断电,数据库会出错,而导致系统无法自动恢复
                if (!Readonly)
                {
                    builder.TryAdd("Synchronous", "Off");
                }
                // Journal Mode的内存设置太激进了,容易出事,关闭
                //if (!builder.ContainsKey("Journal Mode")) builder["Journal Mode"] = "Memory";
                // 数据库中一种高效的日志算法,对于非内存数据库而言,磁盘I/O操作是数据库效率的一大瓶颈。
                // 在相同的数据量下,采用WAL日志的数据库系统在事务提交时,磁盘写操作只有传统的回滚日志的一半左右,大大提高了数据库磁盘I/O操作的效率,从而提高了数据库的性能。
                if (!Readonly)
                {
                    builder.TryAdd("Journal Mode", "WAL");
                }
                // 绝大多数情况下,都是小型应用,发生数据损坏的几率微乎其微,而多出来的问题让人觉得很烦,所以还是采用内存设置
                // 将来可以增加自动恢复数据的功能
                //if (!builder.ContainsKey("Journal Mode")) builder["Journal Mode"] = "Memory";

                if (Readonly)
                {
                    builder.TryAdd("Read Only", "true");
                }

                // 自动清理数据
                if (builder.TryGetAndRemove("autoVacuum", out var vac))
                {
                    AutoVacuum = vac.ToBoolean();
                }
            }
            //else
            //    SupportSchema = false;

            // 默认超时时间
            //if (!builder.ContainsKey("Default Timeout")) builder["Default Timeout"] = 5 + "";

            // 繁忙超时
            //var busy = Setting.Current.CommandTimeout;
            //if (busy > 0)
            {
                // SQLite内部和.Net驱动都有Busy重试机制,多次重试仍然失败,则会出现dabase is locked。通过加大重试次数,减少高峰期出现locked的几率
                // 繁忙超时时间。出现Busy时,SQLite内部会在该超时时间内多次尝试
                //if (!builder.ContainsKey("BusyTimeout")) builder["BusyTimeout"] = 50 + "";
                // 重试次数。SQLite.Net驱动在遇到Busy时会多次尝试,每次随机等待1~150ms
                //if (!builder.ContainsKey("PrepareRetries")) builder["PrepareRetries"] = 10 + "";
            }

            DAL.WriteLog(builder.ToString());
        }