在C#和SQL Server数据库中怎么给DateTime类型赋null值、null和

今天在将数据库里的datetime类型数据赋给C#里DateTime类型变量的时候遇到一个问题,那就是数据库里面的datetime数据存在null值。

C#默认不能将null赋给DateTime变量,但是可以使用Nullable类型的DateTime,只需在声明变量的时候在DateTime后加一个?号,如:

DateTime? x ;

x便可以赋null值。

但是从数据库的datetime类型变为C#的DateTime类型需要Convert.ToDateTime();如果数据库里面的datetime数据为null的话,这个方法是不能使用的。

于是我加了个if语句:

[@more@]

if((row["date1"])!=null)

但是这个if语句无效,改成

row["date1"].ToString()!=null

仍然无法判断数据库中的datetime类型的列date1是否为null。

后来更改为row["date1"].ToString()!=""终于可以判断了。完整语句为:

DateTime? x;

if(row["date1"].ToString()!="")

x=Convert.ToDateTime(["date1"]);

else

x=null;

注意最后x只能赋null,而不能像原来数据库数据一样赋""。因为x被声明为Nullable类型变量的。

这样,如果将x在存入数据库的话,数据库里显示为1900-1-1.

null和""在C#中的区别是前者没有指向任何String对象,而后者为长度为0的String对象,且为其分配了内存空间。

但是刚刚试了一下,不论""或null存入数据库的时候都是1900-1-1,如果想在数据库里也为null的话,可以选择当x为null或""时,不存入数据库中的相关列。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7946503/viewspace-1002551/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7946503/viewspace-1002551/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C#SQL Server类型映射枚举类的代码: ``` public enum SqlType { BigInt, Binary, Bit, Char, Date, DateTime, DateTime2, DateTimeOffset, Decimal, Float, Geography, Geometry, HierarchyId, Image, Int, Money, NChar, NText, Numeric, NVarChar, Real, SmallDateTime, SmallInt, SmallMoney, SqlVariant, SysName, Text, Time, Timestamp, TinyInt, UniqueIdentifier, VarBinary, VarChar, Xml } public static class SqlTypeMapper { public static string Map(SqlType type) { switch (type) { case SqlType.BigInt: return "bigint"; case SqlType.Binary: return "binary"; case SqlType.Bit: return "bit"; case SqlType.Char: return "char"; case SqlType.Date: return "date"; case SqlType.DateTime: return "datetime"; case SqlType.DateTime2: return "datetime2"; case SqlType.DateTimeOffset: return "datetimeoffset"; case SqlType.Decimal: return "decimal"; case SqlType.Float: return "float"; case SqlType.Geography: return "geography"; case SqlType.Geometry: return "geometry"; case SqlType.HierarchyId: return "hierarchyid"; case SqlType.Image: return "image"; case SqlType.Int: return "int"; case SqlType.Money: return "money"; case SqlType.NChar: return "nchar"; case SqlType.NText: return "ntext"; case SqlType.Numeric: return "numeric"; case SqlType.NVarChar: return "nvarchar"; case SqlType.Real: return "real"; case SqlType.SmallDateTime: return "smalldatetime"; case SqlType.SmallInt: return "smallint"; case SqlType.SmallMoney: return "smallmoney"; case SqlType.SqlVariant: return "sql_variant"; case SqlType.SysName: return "sysname"; case SqlType.Text: return "text"; case SqlType.Time: return "time"; case SqlType.Timestamp: return "timestamp"; case SqlType.TinyInt: return "tinyint"; case SqlType.UniqueIdentifier: return "uniqueidentifier"; case SqlType.VarBinary: return "varbinary"; case SqlType.VarChar: return "varchar"; case SqlType.Xml: return "xml"; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } } } ``` 使用时,可以通过 `SqlTypeMapper.Map(SqlType)` 方法来获取对应的SQL Server数据类型。例如: ``` var sqlType = SqlTypeMapper.Map(SqlType.VarChar); ``` 这样就可以得到 `varchar` 这个数据类型了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值