예제 #1
0
        public static IDbContext Context(string ConnName)
        {
            var Log = LogManager.GetLogger(ConnName);
            var setting = ConfigurationManager.ConnectionStrings[ConnName];
            var db = new DbContext().ConnectionString(setting.ConnectionString, Provider(setting.ProviderName));
           
            db.OnExecuting(x =>
            {
                if (APP.OnDbExecuting != null) APP.OnDbExecuting(x);
                var sql = x.Command.CommandText;
                for (int i = x.Command.Parameters.Count - 1;i>=0;i--)
                {
                    var item = x.Command.Parameters[i] as IDataParameter;
                    sql = sql.Replace(item.ParameterName, String.Format("'{0}'", item.Value));
                }
 
                Log.Debug(sql);
            });

            db.OnError(e =>
            {
                var ex = e.Exception as SqlException;
                if (ex != null)
                {
                    var error = "";
                    switch (ex.Number)
                    {
                        case -2:
                            error = "超时时间已到。在操作完成之前超时时间已过或服务器未响应";
                            break;
                        case 4060:
                            // Invalid Database
                            error = "数据库不可用,请检查系统设置后重试!";
                            break;
                        case 18456:
                            // Login Failed
                            error = "登陆数据库失败!";
                            break;
                        case 547:
                            // ForeignKey Violation
                            error = "数据已经被引用,更新失败,请先删除引用数据并重试!";
                            break;
                        case 2627:
                            // Unique Index/Constriant Violation
                            error = "主键重复,更新失败!";
                            break;
                        case 2601:
                            // Unique Index/Constriant Violation   
                            break;
                        default:
                            // throw a general DAL Exception   
                            break;
                    }
                    if (!string.IsNullOrEmpty(error))
                        throw new Exception(error);
                }
            });

            return db;
        }
예제 #2
0
        public static IDbContext Context(string connectionName)
        {
            var Log = LogManager.GetLogger(connectionName);
            var providerName = string.Empty;
            var connectionString = string.Empty;

            if (App.ConnectionHandler != null)
            {
                App.ConnectionHandler(connectionName, ref providerName, ref connectionString);
            }
            else
            {
                var setting = ConfigurationManager.ConnectionStrings[connectionName];
                if (setting == null)
                    throw new Exception(string.Format("未配置连接{0}!",connectionName));
                providerName = setting.ProviderName;
                connectionString = setting.ConnectionString;
            }

            var db = new DbContext().ConnectionString(connectionString, Provider(providerName));
            db.OnExecuting(x =>
            {
                var sql = x.Command.CommandText;

                if (App.EnableZql)
                {
                    //消除字符串影响
                    var values = new List<string>();
                    var matches = new Regex(@"\'(.*?)\'", RegexOptions.Multiline).Matches(sql);
                    for (var i = 0; i < matches.Count; i++)
                    {
                        var matchString = matches[i].Value;
                        if (sql.IndexOf(matchString) > 0) //防止出现相同的字符,被处理两次
                        {
                            sql = sql.Replace(matchString, string.Format("{{{0}}}", values.Count));
                            values.Add(matchString);
                        }
                    }

                    //处理框架定义sql
                    var sType = db.Data.Provider.ToString().Split('.').LastOrDefault().Replace("Provider", "");
                    var dbType = ConvertHelper.ToEnum<DbType>(sType, DbType.Default);

                    matches = new Regex(@"\{([^0-9].*?)\}", RegexOptions.Multiline).Matches(sql);
                    foreach (Match match in matches)
                    {
                        var args = match.Groups[1].Value.Split(',').ToList();
                        var name = args[0];
                        args.RemoveAt(0);
                        var maker = App.DictZql[dbType, name];
                        if (maker == null)
                            throw new Exception(string.Format("未在数据库{0}中注册{1}!", dbType, name));

                        var str = App.DictZql[dbType, name].Handler(args.ToArray());
                        sql = sql.Replace(match.Value, str);
                    }

                    //恢复字符串
                    sql = string.Format(sql, values.ToArray());
                    x.Command.CommandText = sql;
                }

                if (App.OnDbExecuting != null) 
                    App.OnDbExecuting(x);
                 
                //处理输出到日志中的SQL,把参数化的变量替换成字符串
                for (int i = x.Command.Parameters.Count - 1;i>=0;i--)
                {
                    var item = x.Command.Parameters[i] as IDataParameter;
                    sql = sql.Replace(item.ParameterName, String.Format("'{0}'", item.Value));
                }
 
                //写日志
                Log.Debug(sql);
            });

            db.OnError(e =>
            {
                var ex = e.Exception as SqlException;
                if (ex != null)
                {
                    var error = "";
                    switch (ex.Number)
                    {
                        case -2:
                            error = "超时时间已到。在操作完成之前超时时间已过或服务器未响应";
                            break;
                        case 4060:
                            // Invalid Database
                            error = "数据库不可用,请检查系统设置后重试!";
                            break;
                        case 18456:
                            // Login Failed
                            error = "登陆数据库失败!";
                            break;
                        case 547:
                            // ForeignKey Violation
                            error = "数据已经被引用,更新失败,请先删除引用数据并重试!";
                            break;
                        case 2627:
                            // Unique Index/Constriant Violation
                            error = "主键重复,更新失败!";
                            break;
                        case 2601:
                            // Unique Index/Constriant Violation   
                            break;
                        default:
                            // throw a general DAL Exception   
                            break;
                    }
                    if (!string.IsNullOrEmpty(error))
                        throw new Exception(error);
                }
            });

            return db;
        }