- [C#] Sử dụng DotNetBrowser nhân Chromium giải pháp thay thế WebBrowser trên Winform
- [C#] Viết ứng dụng xem và dò kết quả xổ số kiến thiết miền nam
- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
[SQL SERVER] Chia sẽ hàm convert table sang class C#
Bài viết hôm nay, mình sẽ tiếp tục chia sẽ các bạn một hàm trong sqlserver dùng để chuyển một table bất kỳ trong sqlserver thành một class C#.
[SQLSERVER] Convert Table Name to Class C#
Trong lập trình hướng đối tượng C#, các bạn thường tạo ra những Class từ TableName trong database.
Nếu bạn nào đã sử dụng Linq 2 SQL
hay Entity Framework
thì khi visual studio kết nối với database từ nó sẽ tự động sinh ra cho các bạn các class object tương ứng với những table mà bạn liên kết.
Dưới đây là hàm giúp các bạn có thể dễ dàng chuyển table name sang class:
Ví dụ mình demo bên dưới mình sẽ chuyển table name tbl_user
sang class C#
Source code SQL SERVER:
declare @TableName sysname = 'tbl_user'
declare @Result varchar(max) = 'public class ' + @TableName + '
{'
select @Result = @Result + '
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
select
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
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 'string'
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
where object_id = object_id(@TableName)
) t
order by ColumnId
set @Result = @Result + '
}'
print @Result
Kết quả trả về khi chạy câu lệnh sql trên:
public class tbl_user
{
public string tendangnhap { get; set; }
public string matkhau { get; set; }
public bool? truycap { get; set; }
public string sodtgui { get; set; }
public string nguoitd { get; set; }
public string thoigian { get; set; }
public bool? kt { get; set; }
public int? ma { get; set; }
public string maphongban { get; set; }
public string tenphongban { get; set; }
public int? manv { get; set; }
public int? nhomuser { get; set; }
public int? cap { get; set; }
public bool? morong { get; set; }
public string avatar { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public int id { get; set; }
public string email { get; set; }
public bool? online { get; set; }
public bool? sms { get; set; }
public bool? duyet { get; set; }
public string phanhe { get; set; }
public string groupuser { get; set; }
public bool? nhanthongbao { get; set; }
public string vitri { get; set; }
public string makpi { get; set; }
}
HAVE FUN :)