基于sqlserver数据库用C#winform写一个微型orm的代码生成器,期间用到要查询ms数据库的表结构,写一个脚本记录下:
select SO.name as TableName,SC.name as TableCloumn,ST.name as DataType,
(
SELECT COUNT(1) AS Is_PK
FROM syscolumns
JOIN sysindexkeys ON syscolumns.id=sysindexkeys.id AND syscolumns.colid=sysindexkeys.colid
JOIN sysindexes ON syscolumns.id=sysindexes.id AND sysindexkeys.indid=sysindexes.indid
JOIN sysobjects ON sysindexes.name=sysobjects.name AND sysobjects.xtype='PK'
WHERE syscolumns.name=SC.name AND syscolumns.id=object_id(SO.name)
) as IsPrimaryKey
,SC.colid as CloumnIndex
from sysobjects SO
inner join syscolumns SC on SO.id = SC.id and SO.xtype = 'U' and SO.status >= 0 and SO.name= '这里是数据库的表名'
inner join systypes ST on SC.xtype = ST.xusertype
order by SO.name asc, SC.colorder asc
(
SELECT COUNT(1) AS Is_PK
FROM syscolumns
JOIN sysindexkeys ON syscolumns.id=sysindexkeys.id AND syscolumns.colid=sysindexkeys.colid
JOIN sysindexes ON syscolumns.id=sysindexes.id AND sysindexkeys.indid=sysindexes.indid
JOIN sysobjects ON sysindexes.name=sysobjects.name AND sysobjects.xtype='PK'
WHERE syscolumns.name=SC.name AND syscolumns.id=object_id(SO.name)
) as IsPrimaryKey
,SC.colid as CloumnIndex
from sysobjects SO
inner join syscolumns SC on SO.id = SC.id and SO.xtype = 'U' and SO.status >= 0 and SO.name= '这里是数据库的表名'
inner join systypes ST on SC.xtype = ST.xusertype
order by SO.name asc, SC.colorder asc