2011-05-01 54 views
2

查看下面的代码是在T-SQL的快速脚本建立获取一类使用设置属性:创建表名称获取设置属性为

DECLARE @COLUMN_NAME varchar(250) 
DECLARE @DATA_TYPE varchar(250) 
DECLARE c1 CURSOR FOR 

select COLUMN_NAME, DATA_TYPE from information_schema.columns 
where table_name = 'Members' 
OPEN c1 
FETCH NEXT FROM c1 INTO @COLUMN_NAME, @DATA_TYPE 
WHILE @@FETCH_STATUS = 0 
BEGIN 

IF @DATA_TYPE = 'nvarchar' 
BEGIN 
    SET @DATA_TYPE = 'string' 
END 

IF @DATA_TYPE = 'ntext' 
BEGIN 
    SET @DATA_TYPE = 'string' 
END 

IF @DATA_TYPE = 'datetime' 
BEGIN 
    SET @DATA_TYPE = 'DateTime' 
END 


PRINT 'public ' + @DATA_TYPE + ' ' + @COLUMN_NAME + ' { get; set; }' 

FETCH NEXT FROM c1 INTO @COLUMN_NAME, @DATA_TYPE 

END 
CLOSE c1 
DEALLOCATE c1 
GO 

,如果你可以给它添加或清理它,这将是伟大的!

UPDATE 下面的代码工作,我做了一些细微的修改。

DECLARE @Script NVARCHAR(MAX) = '' 

SELECT @Script = @Script + ' 
public ' + CASE WHEN DATA_TYPE IN ('nvarchar','ntext') THEN 'string' 
       WHEN DATA_TYPE = 'datetime' THEN 'DateTime' 
       ELSE DATA_TYPE 
      END 
         + ' ' 
         + upper(substring(COLUMN_NAME,1,1))+ 
         + lower(substring(COLUMN_NAME,2,499)) 
         + ' { get; set; }' 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'SubCategory' 

PRINT @Script 

回答

2

可以摆脱光标与

DECLARE @Script NVARCHAR(MAX) = '' 

SELECT @Script = @Script + ' 
public ' + CASE WHEN DATA_TYPE IN ('nvarchar','ntext') THEN 'string' 
       WHEN DATA_TYPE = 'datetime' THEN 'DateTime' 
       ELSE DATA_TYPE 
      END 
         + ' ' + COLUMN_NAME + ' { get; set; }' 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'Members' 

PRINT @Script 
+1

尼斯缩短了不少。我懒得发起SSMS来完成它; p和+1特别用于修复信息模式表中的外壳 - 在区分大小写模式下很痛苦,否则。 – 2011-05-01 22:40:03

+0

喜欢它!好的一块干净的编码和易于遵循。做得好! – 2011-05-02 13:47:23

2

如果是你所需要的... 停止修修补补; p如果你想反馈:

  • 没有必要的游标;一套基于SELECT应该足够了;任何时候你发现自己写光标是可能错误
  • 要么加入到映射表(SQL类型和C#类之间),或使用CASE ....挑选内嵌
  • 可能想成为偏执保留字,例如public - 在C#中,您需要使用@public(或避免它)
  • 您可能想要删除空格;你可以调用一个数据库列[something with spaces],但不会工作在C#
  • 你可以检查扩展元数据的MS_Description价值,并编写成(的一个,或者两者)///<summary>...</summary>[Description(@"...")]
0
declare @TableName sysname = 'Members' 
declare @result varchar(max) = '' 

select @result = @result + ' 
public ' + ColumnType + ' ' + ColumnName + ' { get; set; } 
' 
from 
(
    select replace(col.name, ' ', '_') ColumnName, 
     case typ.name 
      when 'bigint' then 'long' 
      when 'binary' then 'byte[]' 
      when 'bit' then 'bool' 
      when 'char' then 'char' 
      when 'date' then 'DateTime' 
      when 'datetime' then 'DateTime' 
      when 'datetime2' then 'DateTime' 
      when 'datetimeoffset' then 'DateTimeOffset' 
      when 'decimal' then 'decimal' 
      when 'float' then 'float' 
      when 'image' then 'byte[]' 
      when 'int' then 'int' 
      when 'money' then 'decimal' 
      when 'nchar' then 'char' 
      when 'ntext' then 'string' 
      when 'numeric' then 'decimal' 
      when 'nvarchar' then 'string' 
      when 'real' then 'double' 
      when 'smalldatetime' then 'DateTime' 
      when 'smallint' then 'short' 
      when 'smallmoney' then 'decimal' 
      when 'text' then 'string' 
      when 'time' then 'TimeSpan' 
      when 'timestamp' then 'DateTime' 
      when 'tinyint' then 'byte' 
      when 'uniqueidentifier' then 'Guid' 
      when 'varbinary' then 'byte[]' 
      when 'varchar' then 'string' 
     end ColumnType 
    from sys.columns col 
     join sys.types typ on 
      col.system_type_id = typ.system_type_id 
    where object_id = object_id(@TableName) 
) t 

print @result