打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MSSQL数据库转换为C#类的SQL语句
jcstone  2014-12-18

      将数据库表结构输出为C#类的通用SQL语句。作者根据来源文章进行进一步修改,直接转换SQL数据类型到C#数据类型。


Use YourDataBaseName  --你的数据库名
 

DECLARE @tableName VARCHAR(MAX)
DECLARE @tableCount int
DECLARE @tableIndex int
DECLARE @tempTable TABLE
(
  Header VARCHAR(100),
  DType VARCHAR(100),
  DATA VARCHAR(MAX)
)

SELECT @tableCount=COUNT(*) FROM SysObjects WHERE XType='U'
--XType='U':表示所有用户表; 
--XType='S':表示所有系统表;
SET @tableIndex=1

WHILE @tableIndex<=@tableCount

 BEGIN
  SELECT @tableName=NAME 
  FROM
  (
   SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS TempNO,NAME FROM SysObjects 
   WHERE XType='U'
  ) TEMP
  WHERE TempNO=@tableIndex
 
  INSERT INTO @tempTable VALUES('','','')
 
  INSERT INTO @tempTable VALUES('#region ' ,@tableName,'')
 
  INSERT INTO @tempTable VALUES('public partial class ' ,@tableName,'')
   INSERT INTO @tempTable VALUES( '{','','')

  INSERT INTO @tempTable  select 'public ',data_type , column_name +'{get;set;}'
from information_schema.columns 
where table_name=@tableName and data_type<>'xml'

  INSERT INTO @tempTable VALUES( '}','','')
  
  INSERT INTO @tempTable VALUES('#endregion','','')
  
  INSERT INTO @tempTable VALUES('','','')
  
  SET @tableIndex+=1
 
 
update @tempTable set DType = 'int'
where DType = 'int'

update @tempTable set DType = 'int64'
where DType = 'bigint'

update @tempTable set DType = 'byte[]'
where DType = 'binary'

update @tempTable set DType = 'byte[]'
where DType = 'image'

 
update @tempTable set DType = 'string'
where DType = 'char'

update @tempTable set DType = 'string'
where DType = 'nchar'

update @tempTable set DType = 'string'
where DType ='nvarchar'



update @tempTable set DType = 'string'
where DType = 'text'

update @tempTable set DType = 'string'
where DType = 'ntext'

update @tempTable set DType = 'bool'
where DType ='bit'

update @tempTable set DType = 'double'
where DType ='decimal'

update @tempTable set DType = 'double'
where DType ='numeric'

update @tempTable set DType = 'Single'
where DType ='real'

 
update @tempTable set DType = 'DateTime'
where DType ='date' 

update @tempTable set DType = 'DateTime'
where DType ='datetime' 

update @tempTable set DType = 'DateTime'
where DType ='smalldatetime' 


update @tempTable set DType = 'DateTime'
where DType ='timestamp' 

update @tempTable set DType = 'int16'
where DType ='smallint' 

update @tempTable set DType = 'double'
where DType ='smallmoney' 


update @tempTable set DType = 'double'
where DType ='money'


END

SELECT * FROM @tempTable
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
SQL Server 中 ROWLOCK 行级锁
金蝶sql结转语句
SQL保存查询结果——生成新表
什么时候用${}??
金蝶K3中帐套会计期间的修改
MySql多表关联Update更新sql语句
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服