Ejemplo n.º 1
0
        /// <summary>
        /// Gets sql expression for column
        /// (uses default value or function if available)
        /// </summary>
        /// <returns></returns>
        public string GetColumnExpression()
        {
            string result;

            if (HasDefault)
            {
                result = " isnull(cast(src." + SqlCreator.Brackets(BulkColumnName) + " as " + DataTypeOutput + ") ," + Default + ")";
            }
            else if (HasFunction)
            {
                result = Function + " ";
            }
            else
            { //kein Default Value vorhanden
                result = "src." + SqlCreator.Brackets(BulkColumnName) + " ";
            }

            return(result);
        }
        /// <summary>
        /// Creates an sql column list from a database table
        /// </summary>
        /// <param name="con">sql connection</param>
        /// <param name="destinationTableName">destination table name</param>
        /// <returns>Sql column list</returns>
        public SqlColumnList AddSqlColumnDefinitions(SqlConnection con, string destinationTableName)
        {
            SqlColumnList result = new SqlColumnList();

            try
            {
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }

                DataTable     schema;
                SqlConnection sqlConnnection = con;
                SqlCommand    sqlCom         = sqlConnnection.CreateCommand();


                sqlCom.CommandText = "select TOP 0 * from " + SqlCreator.Brackets(destinationTableName);
                SqlDataReader reader = sqlCom.ExecuteReader(CommandBehavior.KeyInfo);
                schema = reader.GetSchemaTable();


                foreach (DataRow row in schema.Rows)
                {
                    result.Add(row["ColumnName"].ToString(), row["DataTypeName"].ToString(), row["DataType"].ToString(),
                               Int32.Parse(row["ColumnSize"].ToString()), Int32.Parse(row["NumericPrecision"].ToString()),
                               Int32.Parse(row["NumericScale"].ToString()),
                               (row["IsKey"].ToString() == "True"), (row["IsAutoIncrement"].ToString() == "True"), (row["AllowDBNull"].ToString() == "True"));
                }

                sqlConnnection.Close();

                foreach (ColumnConfig config in this.ColumnConfigList)
                {
                    config.SetSqlColumnDefinitions(result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(result);
        }