Exemplo n.º 1
0
        //----------Members-----------

        #region 连接字符串

#if lang_zh
        /// <summary>
        /// 设置连接字符串
        /// </summary>
        /// <param name="m_connectionString">连接字符串</param>
        /// <param name="m_ConnectionAliasName">连接别名;如果没起别名,默认为DEFAULT,会以此作为默认的连接字符串</param>
#else
        /// <summary>
        /// Set the connection string
        /// </summary>
        /// <param name="m_connectionString">connection string</param>
        /// <param name="m_ConnectionAliasName">the Alias Name of Connection (if not pass name,it will use the DEFAULT name instead.)</param>
#endif
        public static void SetConnection(string m_connectionString, string m_ConnectionAliasName = ALIAS_NAME_DEFAULT)
        {
            //检查连接字符串
            SqlConnectionStringBuilder builder = null;

            try
            {
                builder = new SqlConnectionStringBuilder(m_connectionString);
            }
            catch (Exception ex)
            {
#if lang_zh
                const string errMsg = "连接字符串有错误。";
#else
                const string errMsg = "Connection String is invalid.";
#endif

                throw new ArgumentException(errMsg, "m_connectionString", ex);
            }

            //##异步的

            /* 启用异步SQL支持的时候,连接字符串必须包含以下属性。
             * https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnectionstringbuilder.asynchronousprocessing(v=vs.110).aspx
             *
             * 标记为“允许异步”。即:
             * 1.没有标记时,及时使用Command的Async方法,依然是同步操作;
             * 2.有标记时,如果继续使用Command的同步方法,仍然能用于同步操作。
             */
#if NET_STD
#else
            builder.AsynchronousProcessing = true;
#endif

            string finalConnectionString = builder.ToString();

            //检查连接是否可用,同时获取SqlServer的产品版本号
            string version;
            try
            {
                version = AsyncSQLHelper.ExecuteScalar(finalConnectionString, CommandType.Text, "SELECT SERVERPROPERTY('ProductVersion')") as String;
            }
            catch (Exception ex)
            {
#if lang_zh
                const string errMsg = "此连接字符串无法正常工作。";
#else
                const string errMsg = "This Connection String was unable to work properly.";
#endif
                throw new ArgumentException(errMsg, "m_connectionString", ex);
            }

            //记录结果
            var item = new ConnectionRepositoryItem
            {
                ConnectionString     = finalConnectionString,
                ServerProductVersion = new Version(version)
            };
            ConnectionRepository.SetRepositoryItem(m_ConnectionAliasName, item);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <param name="m_ConnectionAliasName"></param>
        /// <returns>连接别名;如果没起别名,则使用默认的连接字符串</returns>
        internal static string GetConnection(string m_ConnectionAliasName = ALIAS_NAME_DEFAULT)
        {
            var targetItem = ConnectionRepository.GetRepositoryItem(m_ConnectionAliasName);

            if (targetItem != null)
            {
                return(targetItem.ConnectionString);
            }
            else
            {
#if lang_zh
                const string errMsg1 = "预期的连接字符串不存在。";
                const string errMsg2 = "连接别名不存在:";
#else
                const string errMsg1 = "the target Connection String is not exist.";
                const string errMsg2 = "the Specified Alias Name was not setted.";
#endif

                throw new InvalidOperationException(errMsg1, new System.ArgumentOutOfRangeException("m_ConnectionAliasName", errMsg2 + m_ConnectionAliasName));
            }
        }