public static SqlParameter ToSqlPrimaryIDParam(this object obj, SqlParameter[] additionalParams = null)
        {
            Type type = obj.GetType();

            var sqlProp = type.GetProperties().FirstOrDefault(x => Attribute.IsDefined(x, typeof(PrimaryID)));

            if (sqlProp != null)
            {
            }
            else
            {
                throw new Exception("No Primary ID Field Defined");
            }

            var v = new QueryParamInfo();

            v.Name = sqlProp.Name;

            v.Value = sqlProp.GetValue(obj);


            var sqlParam = new SqlParameter(v.Name, TypeConvertor.ToSqlDbType(sqlProp.PropertyType))
            {
                Value = v.Value
            };

            return(sqlParam);
        }
        public static List <SqlParameter> ToSqlParamsList(this object obj, SqlParameter[] additionalParams = null)
        {
            var props = (
                from p in obj.GetType().GetProperties(System.Reflection.BindingFlags.Public
                                                      | System.Reflection.BindingFlags.Instance
                                                      | System.Reflection.BindingFlags.DeclaredOnly)
                let nameAttr = p.GetCustomAttributes(typeof(QueryParamNameAttribute), true)
                               let ignoreAttr = p.GetCustomAttributes(typeof(QueryParamIgnoreAttribute), true)
                                                select new { Property = p, Names = nameAttr, Ignores = ignoreAttr }).ToList();

            var result = new List <SqlParameter>();

            foreach (var p in props)
            {
                if (p.Ignores != null && p.Ignores.Length > 0)
                {
                    continue;
                }

                var name  = p.Names.FirstOrDefault() as QueryParamNameAttribute;
                var pinfo = new QueryParamInfo();

                if (name != null && !String.IsNullOrWhiteSpace(name.Name))
                {
                    pinfo.Name = name.Name.Replace("@", "");
                }
                else
                {
                    pinfo.Name = p.Property.Name.Replace("@", "");
                }

                pinfo.Value = p.Property.GetValue(obj) ?? DBNull.Value;
                var sqlParam = new SqlParameter(pinfo.Name, TypeConvertor.ToSqlDbType(p.Property.PropertyType))
                {
                    Value = pinfo.Value
                };

                result.Add(sqlParam);
            }

            if (additionalParams != null && additionalParams.Length > 0)
            {
                result.AddRange(additionalParams);
            }

            return(result);
        }
        private List <ParameterSQL> StoredProcedureParameters(SqlCommand command, string procedureName)
        {
            List <ParameterSQL> returnValue = new List <ParameterSQL>();

            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = procedureName;
            SqlCommandBuilder.DeriveParameters(command);
            foreach (SqlParameter p in command.Parameters)
            {
                returnValue.Add(new ParameterSQL
                {
                    Name      = p.ParameterName,
                    Direction = p.Direction,
                    Size      = p.Size,
                    DataType  = TypeConvertor.ToSqlDbType(p.DbType)
                });
            }

            return(returnValue);
        }
 public SubSonicSqlParameter(string name, object value)
     : base(name, value)
 {
     SqlDbType = TypeConvertor.ToSqlDbType(value.GetType(), SubSonicContext.DbOptions.SupportUnicode);
 }
 public SubSonicSqlParameter(string name, object value, IDbEntityProperty property)
     : base(name, value, property)
 {
     SqlDbType = TypeConvertor.ToSqlDbType(property.DbType, SubSonicContext.DbOptions.SupportUnicode);
 }
Exemple #6
0
        internal void Setup(
            string applicationConfigurationFileFullName,
            bool useInternalRollingFileConfiguration, string businessLogFileFullname, string technicalLogFileFullname,
            bool useInternalAdoNetConfiguration, Type connectionType, string connectionStringName, string logTableName,
            LogLevel businessMinimumLogLevel  = LogLevel.Trace,
            LogLevel technicalMinimumLogLevel = LogLevel.Trace,
            Func <object, Exception, string> exceptionFormatter = null)
        {
            UseInternalBusinessRollingFileConfiguration  = useInternalRollingFileConfiguration && !String.IsNullOrWhiteSpace(businessLogFileFullname);
            UseInternalTechnicalRollingFileConfiguration = useInternalRollingFileConfiguration && !String.IsNullOrWhiteSpace(technicalLogFileFullname);
            UseInternalBusinessAdoNetConfiguration       = useInternalAdoNetConfiguration;

            if (!UseInternalBusinessRollingFileConfiguration && !UseInternalTechnicalRollingFileConfiguration && !UseInternalBusinessAdoNetConfiguration)
            {
                if (!String.IsNullOrWhiteSpace(applicationConfigurationFileFullName))
                {
                    XmlConfigurator.Configure(LogManager.GetRepository(Assembly.GetEntryAssembly()), Parselog4NetConfigFile(applicationConfigurationFileFullName));
                }
                else
                {
                    XmlConfigurator.Configure(LogManager.GetRepository(Assembly.GetEntryAssembly()));
                }
            }
            else
            {
                var log4NetElement = GetDefaultConfiguration(
                    businessLogFileFullname ?? String.Empty,
                    businessMinimumLogLevel,
                    technicalLogFileFullname ?? String.Empty,
                    technicalMinimumLogLevel,
                    connectionType ?? GetType(),
                    connectionStringName ?? String.Empty,
                    logTableName ?? String.Empty);

                if (!UseInternalTechnicalRollingFileConfiguration)
                {
                    var technicalRollingFileAppenderElement = log4NetElement.DescendantsAndSelf().First(e => e.HasAttributes && e.Attributes("name").Any() && e.Attributes("name").ElementAt(0).Value == "TechnicalRollingFileAppender");
                    technicalRollingFileAppenderElement.Remove();

                    var technicalLoggerElement = log4NetElement.DescendantsAndSelf().First(e => e.HasAttributes && e.Attributes("ref").Any() && e.Attributes("ref").ElementAt(0).Value == "TechnicalRollingFileAppender");
                    technicalLoggerElement.Remove();
                }

                if (!UseInternalBusinessRollingFileConfiguration)
                {
                    var businessRollingFileAppenderElement = log4NetElement.DescendantsAndSelf().First(e => e.HasAttributes && e.Attributes("name").Any() && e.Attributes("name").ElementAt(0).Value == "BusinessRollingFileAppender");
                    businessRollingFileAppenderElement.Remove();

                    var businessLoggerElement = log4NetElement.DescendantsAndSelf().First(e => e.HasAttributes && e.Attributes("ref").Any() && e.Attributes("ref").ElementAt(0).Value == "BusinessRollingFileAppender");
                    businessLoggerElement.Remove();
                }

                if (!UseInternalBusinessAdoNetConfiguration)
                {
                    var businessRollingFileAppenderElement = log4NetElement.DescendantsAndSelf().First(e => e.HasAttributes && e.Attributes("name").Any() && e.Attributes("name").ElementAt(0).Value == "BusinessAdoNetAppender");
                    businessRollingFileAppenderElement.Remove();

                    var businessLoggerElement = log4NetElement.DescendantsAndSelf().First(e => e.HasAttributes && e.Attributes("ref").Any() && e.Attributes("ref").ElementAt(0).Value == "BusinessAdoNetAppender");
                    businessLoggerElement.Remove();
                }

                XmlConfigurator.Configure(LogManager.GetRepository(Assembly.GetCallingAssembly()), log4NetElement.ToXmlElement());
            }

            var loggers = LogManager.GetCurrentLoggers(Assembly.GetCallingAssembly());

            if (!IsBusinessLogTableCreated)
            {
                AdoNetAppender adoNetAppender = null;
                if (loggers.Any())
                {
                    foreach (var adoAppender in loggers.Select(
                                 logger => logger.Logger.Repository.GetAppenders().FirstOrDefault(a => a.GetType() == typeof(AdoNetAppender)))
                             .Where(adoAppender => adoAppender != null).OfType <AdoNetAppender>())
                    {
                        adoNetAppender = adoAppender;
                        break;
                    }
                }

                if (adoNetAppender != null)
                {
                    var connectionString = ConfigurationManager.ConnectionStrings[adoNetAppender.ConnectionStringName].ConnectionString;
                    var field            = typeof(AdoNetAppender).GetField("m_parameters", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                    if (field == null)
                    {
                        Debug.WriteLine("Can not find field name 'm_parameters' in AdoNetAppender class using reflection.");
                        throw new InvalidOperationException("Can not find field name 'm_parameters' in AdoNetAppender class using reflection.", new NullReferenceException("field"));
                    }

                    if (field.GetValue(adoNetAppender) is ArrayList parameters)
                    {
                        var fields = adoNetAppender.CommandText.Split(' ')[3].Split(',').Select(e => e.Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "")).ToList();
                        logTableName = adoNetAppender.CommandText.Split(' ')[2];

                        var tableFieldsList = new List <string> {
                            "[LOG_id] [bigint] IDENTITY(1,1) NOT NULL"
                        };
                        for (var i = 0; i < parameters.Count; i++)
                        {
                            var parameter  = (AdoNetAppenderParameter)parameters[i];
                            var tableField = new StringBuilder();

                            tableField.AppendFormat("[{0}] [{1}]{2} {3}",
                                                    fields[i],
                                                    TypeConvertor.ToSqlDbType(parameter.DbType),
                                                    parameter.DbType == DbType.AnsiString || parameter.DbType == DbType.AnsiStringFixedLength ||
                                                    parameter.DbType == DbType.String || parameter.DbType == DbType.StringFixedLength ||
                                                    parameter.DbType == DbType.Xml
                                                                        ? String.Format("({0})", parameter.Size)
                                                                        : String.Empty,
                                                    "NULL");

                            tableFieldsList.Add(tableField.ToString());
                        }

                        using (var connection = new SqlConnection(connectionString))
                        {
                            try
                            {
                                connection.Open();
                                using (var command = connection.CreateCommand())
                                {
                                    var nameParameter = command.CreateParameter();
                                    nameParameter.DbType        = DbType.String;
                                    nameParameter.ParameterName = "name";
                                    nameParameter.Value         = logTableName.ToLower();
                                    command.Parameters.Add(nameParameter);

                                    command.CommandText  = "IF NOT EXISTS (SELECT * FROM sys.tables WHERE LOWER(name) = @name) " + Environment.NewLine;
                                    command.CommandText += "BEGIN " + Environment.NewLine;
                                    command.CommandText += String.Format("CREATE TABLE {0} (", logTableName) + Environment.NewLine;

                                    foreach (var tableField in tableFieldsList)
                                    {
                                        command.CommandText += tableField + ", " + Environment.NewLine;
                                    }

                                    command.CommandText += String.Format("CONSTRAINT [PK_dbo.{0}] PRIMARY KEY CLUSTERED ", logTableName) + Environment.NewLine;
                                    command.CommandText += "( " + Environment.NewLine;
                                    command.CommandText += "[LOG_id] ASC " + Environment.NewLine;
                                    command.CommandText += ")WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] " + Environment.NewLine;
                                    command.CommandText += ") ON [PRIMARY] " + Environment.NewLine;
                                    command.CommandText += "END";

                                    Debug.WriteLine("Creating logs table: " + command.CommandText);

                                    command.ExecuteNonQuery();
                                }
                            }
                            finally
                            {
                                if (connection.State == ConnectionState.Open)
                                {
                                    connection.Close();
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(@"dbCommand is null.");
                    }
                }
            }

            IsBusinessLogTableCreated = true;
        }