コード例 #1
0
ファイル: Generator.cs プロジェクト: jojozhuang/Projects
        /// <summary>
        /// GenerateXmlElementFromColumn generate a xml element from column's definition.
        /// </summary>
        /// <param name="schema">schema name,</param>
        /// <param name="table">table name</param>
        /// <param name="c">column</param>
        /// <param name="keepSymbol">whether keep symbol when convert column anme to property name.</param>
        /// <param name="xtw">xml text writer</param>
        /// <param name="elementName">name of the xml element</param>
        /// <param name="sqlOnly">whether only generate the xml attributes that related to sql.</param>
        private static void GenerateXmlElementFromColumn(ColumnInfo column, System.Xml.XmlTextWriter xtw)
        {
            string initialValue, cSharpType, sqlDefaultValue;


            initialValue = "";
            cSharpType = "";


            #region initial type depended attributes
            switch (GetDataType(column.DataType))
            {
                case SqlDataType.BigInt:
                    cSharpType = "long";
                    initialValue = "0";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(long.MaxValue).Length + 1;
                    break;

                case SqlDataType.Int:
                    cSharpType = "int";
                    initialValue = "0";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(int.MaxValue).Length + 1;
                    break;

                case SqlDataType.SmallInt:
                    cSharpType = "short";
                    initialValue = "0";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(short.MaxValue).Length + 1;
                    break;

                case SqlDataType.TinyInt:
                    cSharpType = "byte";
                    initialValue = "0";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(byte.MaxValue).Length;
                    break;

                case SqlDataType.Bit:
                    cSharpType = "bool";
                    initialValue = "false";
                    sqlDefaultValue = "0";
                    //uiLength = 5;
                    break;

                case SqlDataType.Decimal:
                case SqlDataType.Numeric:
                    cSharpType = "decimal";
                    initialValue = "0M";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(decimal.MaxValue).Length + 1;
                    //sqlType += "(" + c.DataType.NumericPrecision.ToString() + ", " + c.DataType.NumericScale.ToString() + ")";
                    break;

                case SqlDataType.Money:
                case SqlDataType.SmallMoney:
                    cSharpType = "decimal";
                    initialValue = "0M";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(decimal.MaxValue).Length + 1;
                    break;

                case SqlDataType.Float:
                    cSharpType = "float";
                    initialValue = "0D";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(double.MaxValue).Length + 1;
                    break;

                case SqlDataType.Real:
                    cSharpType = "float";
                    initialValue = "0F";
                    sqlDefaultValue = "0";
                    //uiLength = Utility.SafeToString(double.MaxValue).Length + 1;
                    break;

                case SqlDataType.DateTime:
                case SqlDataType.SmallDateTime:
                    cSharpType = "DateTime";
                    initialValue = "System.DateTime.MinValue";
                    sqlDefaultValue = "null";
                    //uiLength = 10;
                    break;

                case SqlDataType.UniqueIdentifier:
                    cSharpType = "System.Guid";
                    initialValue = "System.Guid.Empty";
                    sqlDefaultValue = "null";
                    //uiLength = 32;
                    break;

                case SqlDataType.Char:
                case SqlDataType.NChar:
                case SqlDataType.VarChar:
                case SqlDataType.VarCharMax:
                case SqlDataType.NVarChar:
                case SqlDataType.NVarCharMax:
                case SqlDataType.Text:
                case SqlDataType.NText:
                    cSharpType = "string";
                    initialValue = "\"\"";
                    sqlDefaultValue = "''''";
                    //uiLength = c.DataType.MaximumLength;
                    //if (uiLength <= 0) uiLength = 0;
                    //if (c.DataType.SqlDataType != SqlDataType.Text
                    //    && c.DataType.SqlDataType != SqlDataType.NText)
                    //{
                    //    if (c.DataType.MaximumLength > 0)
                    //        sqlType += "(" + c.DataType.MaximumLength.ToString() + ")";
                    //    else
                    //    {
                    //        sqlType = sqlType.Remove(sqlType.Length - 3) + "(max)";
                    //    }
                    //}
                    break;

                //following SqlDataType are not processed in this version
                case SqlDataType.Binary:
                case SqlDataType.Image:
                case SqlDataType.SysName:
                case SqlDataType.Timestamp:
                case SqlDataType.VarBinary:
                case SqlDataType.VarBinaryMax:
                case SqlDataType.Variant:
                case SqlDataType.Xml:
                case SqlDataType.UserDefinedType:
                case SqlDataType.UserDefinedDataType:
                    cSharpType = "string";
                    initialValue = "\"\"";
                    sqlDefaultValue = "''''";
                    break;

                default:
                    cSharpType = "string";
                    initialValue = "\"\"";
                    sqlDefaultValue = "''''";
                    break;
            }

            #endregion

            //#region process nullable
            //if (c.Nullable)
            //{
            //    //if (!type.Equals("string", StringComparison.InvariantCultureIgnoreCase))
            //    //{
            //    //    type = string.Concat(type, "?");
            //    //}
            //    initialValue = "null";
            //    sqlDefaultValue = "null";
            //}
            //#endregion


            #region write xml element
            xtw.WriteStartElement("property");
            xtw.WriteAttributeString("sequence", column.Sequence.ToString());
            xtw.WriteAttributeString("columnName", column.ColumnName);
            xtw.WriteAttributeString("dataType", column.DataType);
            xtw.WriteAttributeString("columnLength", column.ColumnLength.ToString());
            xtw.WriteAttributeString("precisionLength", column.PrecisionLength.ToString());
            xtw.WriteAttributeString("scale", column.Scale.ToString());
            xtw.WriteAttributeString("defaultValue", column.DefaultValue);
            xtw.WriteAttributeString("columnDescription", column.ColumnDescription);
            xtw.WriteAttributeString("isIdentity", column.IsIdentity.ToString(System.Globalization.CultureInfo.InvariantCulture));
            xtw.WriteAttributeString("isPrimaryKey", column.IsPrimaryKey.ToString(System.Globalization.CultureInfo.InvariantCulture));
            xtw.WriteAttributeString("isNullable", column.IsNullable.ToString(System.Globalization.CultureInfo.InvariantCulture));
            xtw.WriteAttributeString("lowerName", column.ColumnName.ToLower());
            xtw.WriteAttributeString("field", string.Concat("_", column.ColumnName.ToLower()));
            if (column.DataType == "nvarchar" || column.DataType == "varchar")
                xtw.WriteAttributeString("sqlParameter", GetParameter(column.DataType, column.PrecisionLength));
            else
                xtw.WriteAttributeString("sqlParameter", GetParameter(column.DataType, column.ColumnLength));
            xtw.WriteAttributeString("csharptype", cSharpType);
            xtw.WriteAttributeString("getMethod", UtilityHelper.GetXxxMethod(column));
            xtw.WriteAttributeString("pattern", UtilityHelper.GetPattern(column.DataType));
            xtw.WriteAttributeString("initialValue", initialValue);
            xtw.WriteEndElement();
            #endregion
        }
コード例 #2
0
ファイル: DatabaseCtrl.cs プロジェクト: jojozhuang/Projects
        public static Collection<ColumnInfo> GetColumns(string connectionstring, string tablename)
        {
            Collection<ColumnInfo> columns = new Collection<ColumnInfo>();

            DataTable dt = Sql2kDriver.GetColumns(connectionstring, tablename);

            foreach (DataRow dataRow in dt.Rows)
            {
                ColumnInfo column = new ColumnInfo();
                column.Sequence = DataConvert.GetInt32(dataRow["Sequence"]);
                column.ColumnName = DataConvert.GetString(dataRow["Column_Name"]);
                column.DataType = DataConvert.GetString(dataRow["Data_Type"]);
                column.ColumnLength = DataConvert.GetInt32(dataRow["Column_Length"]);
                column.PrecisionLength = DataConvert.GetInt32(dataRow["Precision_Length"]);
                column.Scale = DataConvert.GetInt32(dataRow["Scale"]);
                column.DefaultValue = DataConvert.GetString(dataRow["Default_Value"]);
                column.ColumnDescription = DataConvert.GetString(dataRow["Column_Description"]);
                column.IsIdentity = DataConvert.GetBool(DataConvert.GetInt32(dataRow["IsIdentity"]));
                column.IsPrimaryKey = DataConvert.GetBool(DataConvert.GetInt32(dataRow["IsPrimaryKey"]));
                column.IsNullable = DataConvert.GetBool(DataConvert.GetInt32(dataRow["IsNullable"]));
                columns.Add(column);
            }

            return columns;
        }