日期:2014-05-18  浏览次数:20414 次

想做一个从数据库取字段自动生成实体的工具,请问该如何获取数据库的字段的"列明"和"描述信息"啊(内详)
比如说数据库字段为
字段               类型                         描述
UserID           int                           用户ID
UserName       varchar(30)           用户名

我该怎样获取数据库中的字段,类型.描述啊
请高手指点

------解决方案--------------------
Select
表名=Case When A.ColOrder=1 Then D.Name Else ' ' End,
字段序号=A.ColOrder,
字段名=A.Name,
标识=Case When COLUMNPROPERTY( A.ID,A.Name, 'IsIdentity ')=1 Then '√ 'Else ' ' End,
主键=Case When exists(Select 1 From sysobjects where Xtype= 'PK ' And Name In (
Select Name From sysindexes Where indid In(
Select indid From sysindexkeys Where ID = A.ID AND colid=A.colid
))) Then '√ ' Else ' ' End,
类型=B.Name,
占用字节数=A.length,
长度=COLUMNPROPERTY(A.ID,A.Name, 'PRECISION '),
小数位数=IsNull(COLUMNPROPERTY(A.ID,A.Name, 'Scale '),0),
允许空=Case When A.isnullable=1 Then '√ 'Else ' ' End,
默认值=IsNull(E.text, ' '),
字段说明=IsNull(G.[value], ' ')
From syscolumns A
Left Join systypes B On A.Xtype=B.xusertype
Inner Join sysobjects D On A.ID=D.ID And D.Xtype= 'U ' And D.Name <> 'dtproperties '
Left Join syscomments E On A.cdefault=E.ID
Left Join sysproperties G On A.ID=G.ID And A.colid=G.smallid
Where D.Name= 'yourtablename ' --你要查詢的表名
Order By A.ID,A.ColOrder
------解决方案--------------------
精簡一下

Select
表名=Case When A.ColOrder=1 Then D.Name Else ' ' End,
字段序号=A.ColOrder,
字段名=A.Name,
类型=B.Name,
占用字节数=A.length,
长度=COLUMNPROPERTY(A.ID,A.Name, 'PRECISION '),
小数位数=IsNull(COLUMNPROPERTY(A.ID,A.Name, 'Scale '),0),
允许空=Case When A.isnullable=1 Then '√ 'Else ' ' End,
字段说明=IsNull(G.[value], ' ')
From syscolumns A
Left Join systypes B On A.Xtype=B.xusertype
Inner Join sysobjects D On A.ID=D.ID And D.Xtype= 'U ' And D.Name <> 'dtproperties '
Left Join sysproperties G On A.ID=G.ID And A.colid=G.smallid
Where D.Name= 'yourtablename ' --你要查詢的表名
Order By A.ID,A.ColOrder