c mysql isdbnull_解决CodeSmith不能读取MySql数据库字段说明的问题

1 ///2 ///Gets the extended properties for a given schema object.3 ///4 ///The connection string used to connect to the target database.5 ///6 ///7 publicExtendedProperty[] GetExtendedProperties(stringconnectionString, SchemaObjectBase schemaObject)8 {9 ListextendedProperties=newList();10 11 if(schemaObjectisColumnSchema)12 {13 ColumnSchema columnSchema=schemaObjectasColumnSchema;14 15 stringcommandText=string.Format(@"SELECT EXTRA, COLUMN_DEFAULT, COLUMN_TYPE, COLUMN_COMMENT16 FROM INFORMATION_SCHEMA.COLUMNS17 WHERE TABLE_SCHEMA = '{0}'18 AND TABLE_NAME = '{1}'19 AND COLUMN_NAME = '{2}'", columnSchema.Table.Database.Name, columnSchema.Table.Name, columnSchema.Name);20 21 using(DbConnection connection=CreateConnection(connectionString))22 {23 connection.Open();24 25 DbCommand command=connection.CreateCommand();26 command.CommandText=commandText;27 command.Connection=connection;28 29 using(IDataReader reader=command.ExecuteReader(CommandBehavior.CloseConnection))30 {31 while(reader.Read())32 {33 stringextra=reader.GetString(0).ToLower();34 boolcolumndefaultisnull=reader.IsDBNull(1);35 stringcolumndefault="";36 if(!columndefaultisnull)37 {38 columndefault=reader.GetString(1).ToUpper();39 }40 stringcolumntype=reader.GetString(2).ToUpper();41 stringcolumncomment=reader.GetString(3);42 43 boolisIdentity=(extra.IndexOf("auto_increment")>-1);44 extendedProperties.Add(newExtendedProperty("CS_IsIdentity", isIdentity, columnSchema.DataType));45 46 if(isIdentity)47 {48 /*49 MySQL auto_increment doesn't work exactly like SQL Server's IDENTITY50 I believe that auto_increment is equivalent to IDENTITY(1, 1)51 However, auto_increment behaves differently from IDENTITY when used52 with multi-column primary keys.  See the MySQL Reference Manual for details.53 */54 extendedProperties.Add(newExtendedProperty("CS_IdentitySeed",1, columnSchema.DataType));55 extendedProperties.Add(newExtendedProperty("CS_IdentityIncrement",1, columnSchema.DataType));56 }57 58 extendedProperties.Add(newExtendedProperty("CS_ColumnDefaultIsNull", columndefaultisnull, DbType.Boolean));59 extendedProperties.Add(newExtendedProperty("CS_ColumnDefault", columndefault, DbType.String));60 extendedProperties.Add(newExtendedProperty("CS_ColumnType", columntype, DbType.String));61 extendedProperties.Add(newExtendedProperty("CS_ColumnExtra", extra.ToUpper(), DbType.String));62 extendedProperties.Add(newExtendedProperty("CS_Description", columncomment, DbType.String));63 }64 65 if(!reader.IsClosed)66 reader.Close();67 }68 69 if(connection.State!=ConnectionState.Closed)70 connection.Close();71 }72 }73 if(schemaObjectisTableSchema)74 {75 TableSchema tableSchema=schemaObjectasTableSchema;76 stringcommandText=string.Format(@"SHOW CREATE TABLE `{0}`.`{1}`", tableSchema.Database.Name, tableSchema.Name);77 78 using(DbConnection connection=CreateConnection(connectionString))79 {80 connection.Open();81 82 DbCommand command=connection.CreateCommand();83 command.CommandText=commandText;84 command.Connection=connection;85 86 using(IDataReader reader=command.ExecuteReader(CommandBehavior.CloseConnection))87 {88 while(reader.Read())89 {90 stringcreatetable=reader.GetString(1);91 extendedProperties.Add(newExtendedProperty("CS_CreateTableScript", createtable, DbType.String));92 }93 94 if(!reader.IsClosed)95 reader.Close();96 }97 98 if(connection.State!=ConnectionState.Closed)99 connection.Close();100 }101 }102 103 returnextendedProperties.ToArray();104 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值