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物件程式設計
- JavaScript私有屬性和靜態屬性JavaScript
- 私有屬性
- JavaScript公有屬性、私有屬性、公有方法和原型方法JavaScript原型
- 修改物件私有屬性物件
- C#屬性和lamdaC#
- js 私有方法屬性和公有方法屬性簡單介紹JS
- SQL Server 中的 ACID 屬性SQLServer
- 私有setter公有getter屬性
- defer 屬性和 async 屬性
- C#反射設定屬性值和獲取屬性值C#反射
- C#索引器和屬性C#索引
- Python中訪問私有屬性和私有方法Python
- sql server 大資料量表修屬性SQLServer大資料
- c# form窗體modifiers屬性C#ORM
- 協議中的私有屬性協議
- c# hasvalue屬性C#
- 自動生成屬性
- vue計算屬性和vue實力的屬性和方法Vue
- Python 訪問和設定私有屬性Python
- c# winform窗體相關屬性C#ORM
- 私有屬性的Get Set 與 Public
- 常用的瀏覽器私有屬性瀏覽器
- [引]C# DllImport 屬性C#Import
- CMake 屬性之全域性屬性
- js 物件方法、類方法、原型方法的區別;私有屬性、公有屬性、公有靜態屬性的區別JS物件原型
- Python中的私有屬性與私有方法Python
- 庫和屬性
- 屬性和方法
- C#訪問或修改私有類、函式、變數、屬性C#函式變數
- Cardboard對像的公共方法與屬性
- 實驗6 方法和屬性
- javaScript事件(四)event的公共成員(屬性和方法)JavaScript事件
- iOS動畫 屬性屬性解析iOS動畫
- javascript模擬實現私有屬性簡單介紹JavaScript
- C#中屬性的解析C#
- C#屬性與欄位C#
- 瞭解下C# 屬性(Property)C#