为类中的视图创建从表名称获取设置属性

发布于 2024-11-04 09:49:51 字数 1335 浏览 1 评论 0原文

下面的代码是 T-SQL 中的一个快速脚本,它构建了在 CLASS 中使用的 Get Set 属性:

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

如果您可以添加它或清理它,那就太棒了!

更新 下面的代码可以工作,我做了一些轻微的修改。

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

The below code is a quick script in T-SQL which builds Get Set properties for use in a CLASS:

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

IF you can add to it or clean it up, it would be great!

UPDATE
The below code is working and I made some slight modification.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘味果▽酱 2024-11-11 09:49:51

您可以摆脱光标并将其缩短很多

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

You can get rid of the cursor and shorten it quite a lot with

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
原谅我要高飞 2024-11-11 09:49:51

如果它满足您的需要...停止修补 ;p 如果您需要反馈:

  • 不需要该光标;基于集合的 SELECT 就足够了;每当您发现自己编写游标时,
  • 连接到映射表(SQL 类型和 C# 类型之间)或使用 CASE .... 来选择内联
  • 都可能是错误的您可能想对保留字保持偏执,例如public - 在C#中您需要使用@public(或者避免它)
  • 你可能想删除空格;您可以调用数据库列[带有空格的内容],但这在 C# 中不起作用
  • 您可以检查 MS_Description 值的扩展元数据,并将其写入 (其中之一或两者)///
    ...

    [Description(@"...")]

If it does what you need... stop tinkering ;p If you want feedback:

  • no need for that cursor; a set-based SELECT should suffice; anytime you find yourself writing a cursor it is probably wrong
  • either join to a mapping table (between SQL types and C# types), or use CASE .... to pick inline
  • you might want to be paranoid about reserved words, for example public - in C# you'd need to use @public for that (or avoid it)
  • you might want to remove spaces; you can call a db column [something with spaces], but that won't work in C#
  • you could check the extended metadata for an MS_Description value, and write that into (one of, or perhaps both) the ///<summary>...</summary> or [Description(@"...")]
静赏你的温柔 2024-11-11 09:49:51
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文