SQL Server 生成C#公共實體屬性和私有屬性
declare @TableName sysname = 'checkDate'--我的表名
SELECT 'private '+ case [Types].name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
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'
else 'UNKNOWN_' + [Types].name
end +' _'+ [Columns].name+';' FROM sys.tables AS [Tables]
INNER JOIN sys.columns AS [Columns] ON [Tables].object_id = [Columns].object_id
INNER JOIN sys.types AS [Types] ON [Columns].system_type_id = [Types].system_type_id
AND is_user_defined = 0 AND [Types].name <> 'sysname'
LEFT OUTER JOIN sys.extended_properties AS [Properties] ON [Properties].major_id = [Tables].object_id
AND [Properties].minor_id = [Columns].column_id AND [Properties].name = 'MS_Description'
WHERE [Tables].name =@TableName ORDER BY [Columns].column_id
-----------------------------------------------私有實體-------------------------------------------------
declare @Result varchar(max) = '
/// <summary>
/// ' + @TableName +
'
/// </summary>
public class ' + @TableName + '
{'
select @Result = @Result + '
/// <summary>
/// ' + CONVERT(NVARCHAR(500), ISNULL(ColName, '無')) +
'
/// </summary>
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' {
get{return _'+ColumnName+';}
set{_'+ColumnName+'=value;} }'
from
(
SELECT
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
prop.value ColName,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
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'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id
where object_id = object_id(@TableName)
) t
--order by ColumnId
set @Result = @Result + '
}'
print @Result
-----------查詢結果----------
private int _sid;
private int _suid;
/// <summary>
/// checkDate
/// </summary>
public class checkDate
{
/// <summary>
/// 主鍵
/// </summary>
public int sid {
get{return _sid;}
set{_sid=value;} }
/// <summary>
/// 使用者id
/// </summary>
public int suid {
get{return _suid;}
set{_suid=value;} }
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31486267/viewspace-2740018/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python - 物件導向程式設計 - 公共屬性、保護屬性、私有屬性Python物件程式設計
- 私有屬性
- SQL Server 中的 ACID 屬性SQLServer
- C#屬性和lamdaC#
- C#反射設定屬性值和獲取屬性值C#反射
- defer 屬性和 async 屬性
- Python中訪問私有屬性和私有方法Python
- 私有setter公有getter屬性
- Python 訪問和設定私有屬性Python
- vue計算屬性和vue實力的屬性和方法Vue
- 協議中的私有屬性協議
- Python中的私有屬性與私有方法Python
- 私有屬性的Get Set 與 Public
- CMake 屬性之全域性屬性
- C#訪問或修改私有類、函式、變數、屬性C#函式變數
- 屬性和方法
- C#中屬性的解析C#
- C#屬性與欄位C#
- 瞭解下C# 屬性(Property)C#
- HCNP Routing&Switching之BGP團體屬性和團體屬性過濾器過濾器
- python呼叫私有屬性的方法總結Python
- CSS 屬性篇(七):Display屬性CSS
- CMake 屬性之目錄屬性
- CMake 屬性之目標屬性
- C#裡面標籤的屬性和事件C#事件
- 屬性和監聽
- C# 類相同屬性賦值C#賦值
- 【Case】SQL SERVER 屬性OWNER不可用於資料庫xxx。SQLServer資料庫
- node實現檔案屬性批量修改(時間屬性)
- Python的tkinter獲取元件屬性和設定元件屬性Python元件
- css可繼承屬性和非繼承屬性一覽CSS繼承
- Python類屬性和例項屬性分別是什麼?Python
- Python __dict__屬性:檢視物件內部所有屬性名和屬性值組成的字典Python物件
- BGP團體屬性、CommunityUnity
- C# 獲取修改了哪些屬性C#
- Winform Anchor和Dock屬性ORM
- calcMode, keyTimes和keySplines 屬性
- SVG repeatCount和repeatDur屬性SVG