- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
[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 :)