为类中的视图创建从表名称获取设置属性
下面的代码是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以摆脱光标并将其缩短很多
You can get rid of the cursor and shorten it quite a lot with
如果它满足您的需要...停止修补 ;p 如果您需要反馈:
public
- 在C#中您需要使用@public
(或者避免它)[带有空格的内容]
,但这在 C# 中不起作用MS_Description
值的扩展元数据,并将其写入 (其中之一或两者)///
...
或
[Description(@"...")]
If it does what you need... stop tinkering ;p If you want feedback:
SELECT
should suffice; anytime you find yourself writing a cursor it is probably wrongCASE ....
to pick inlinepublic
- in C# you'd need to use@public
for that (or avoid it)[something with spaces]
, but that won't work in C#MS_Description
value, and write that into (one of, or perhaps both) the///<summary>...</summary>
or[Description(@"...")]