コード例 #1
0
        private void LoadFieldDefs(InDbTableDef tableDef)
        {
            SqlCommand command = this.Connection.CreateCommand();

            command.CommandText = string.Format("SELECT\tCOLUMN_NAME, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH,  DATA_TYPE, NUMERIC_PRECISION, NUMERIC_SCALE  FROM INFORMATION_SCHEMA.COLUMNS  WHERE TABLE_NAME = '{0}' AND TABLE_SCHEMA = '{1}'  ORDER BY ORDINAL_POSITION", (object)tableDef.Name, (object)this.GetSchemaName());
            using (IDataReader dataReader = (IDataReader)command.ExecuteReader())
            {
                object[] values = new object[6];
                while (dataReader.Read())
                {
                    dataReader.GetValues(values);
                    int      size         = 0;
                    string   name         = values[0].ToString();
                    DataType inDbDataType = InDbSqlDatabase.SqlDataTypeToInDbDataType(values[3].ToString(), values[5] == DBNull.Value ? 0 : Convert.ToInt32(values[5]));
                    if (inDbDataType == DataType.String)
                    {
                        size = values[2] == DBNull.Value ? 0 : Convert.ToInt32(values[2]);
                    }
                    tableDef.FieldDefs.Add(new InDbFieldDef(name, inDbDataType, size, false));
                }
            }
        }
コード例 #2
0
        protected void CreateTable(InDbTableDef tableDef)
        {
            StringCollection stringCollection = new StringCollection();

            foreach (InDbFieldDef fieldDef in tableDef.FieldDefs)
            {
                stringCollection.Add(string.Format("[{0}] {1}", (object)fieldDef.Name, (object)InDbSqlDatabase.InDbFieldDefToSqlDataType(fieldDef)));
            }
            string str = string.Format("CREATE TABLE {0}.[{1}] ({2}", (object)this.GetSchemaName(), (object)tableDef.Name, (object)StrUtils.Join((IList)stringCollection, "{0}", ","));

            if (tableDef.PrimaryKey != null)
            {
                str = str + ", PRIMARY KEY (" + StrUtils.Join((IList)tableDef.PrimaryKey.FieldDefs.GetFieldNames(), "[{0}]", ",") + ")";
            }
            this.Execute(str + ")");
            try
            {
                for (int index = 0; index < tableDef.IndexDefs.Count; ++index)
                {
                    InDbIndexDef indexDef = tableDef.IndexDefs[index];
                    if (indexDef != tableDef.PrimaryKey)
                    {
                        indexDef.FName = "Index" + (object)index;
                        this.CreateIndex(tableDef.Name, indexDef);
                    }
                }
            }
            catch (Exception ex)
            {
                this.DeleteTable(tableDef.Name);
                throw;
            }
        }