1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| // 新建一个文件, 写入以下内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NullLib.DbHelper
{
public static class DbExt
{
public static T Cast<T>(this object obj) // 使用 default 默认值
{
if (obj is T rst)
return rst;
return default;
}
public static T Cast<T>(this object obj, T dft) // 用户指定默认值
{
if (obj is T rst)
return rst;
return dft;
}
public static object CastDb(this object obj) // 将CLR对象转换为数据库类型
{
if (obj is null)
return DBNull.Value;
return obj;
}
}
}
|