C#中往数据库插入/更新时候关于NUll空值的处理 | 您所在的位置:网站首页 › 数据库空值怎么输入 › C#中往数据库插入/更新时候关于NUll空值的处理 |
本文转载:http://blog.csdn.net/chybaby/article/details/2338943 今天碰到个问题。。SqlCommand对传送的参数中如果字段的值是NULL具然不进行更新操作,也不提示任何错误。。。百思不得其解。。。先作个记录,再查资料看看什么原因。 暂时的解决方法: 1、Update不支持更新Null,先Delete后Insert来替换. 2、替代Null的方法,对于字符型,只要是Null,改为空,语句中就是''. 找到了相关的解决方法 ADO.Net的Command对象如何向数据库插入NULL值(原创) 一般来说,在Asp.Net与数据库的交互中,通常使用Command对象,如:SqlCommand。通过Command对象对数据库操作是相当安全和方便的(相对于RecordSet方式)。但是,同时发现了一个问题。像有些日期字段,如果用户没有选择日期,我们希望他保持NULL状态。我写的关键代码如下: SqlCommand sqlCmd = new SqlCommand(sqlStatment, dbConn); sqlCmd.Parameters.AddWithValue("@Name", name); sqlCmd.Parameters.AddWithValue("@Surname", surname); 这时,虽未出错,但返回的影响行数告诉我。更新未成功。这是怎么回事呢? 原来ADO.Net为了防止一些不容易找出的错误,在Command操作时加了一些限制。我们必须明确指示Command对象,我们需要插入NUll值。修改后的代码如下: SqlCommand sqlCmd = new SqlCommand(sqlStatment, dbConn); sqlCmd.Parameters.AddWithValue("@Name", name); sqlCmd.Parameters.AddWithValue("@Surname", surname); sqlCmd.Parameters[0].IsNullable = true; sqlCmd.Parameters[1].IsNullable = true; 不过,还有一点要注意的就是,这里的IsNullable,不是说你可以插入null值,而是指DBNull.Value值。 希望这点小经验会对大家有帮助。 代码语言:javascript复制方法一、 public int UpdateFeedBackStatus(int _feedBackID, int _status, object _RequestDateTime) { SqlParameter[] param = { new SqlParameter("@FeedBackID", _feedBackID), new SqlParameter("@FeedBackStatusID", _status), new SqlParameter("@RequestDateTime", _RequestDateTime) }; StringBuilder strSql = new StringBuilder(); strSql.Append("UPDATE dbo.FeedBack "); strSql.Append("SET FeedBackStatusID=@FeedBackStatusID,RequestDateTime=@RequestDateTime "); strSql.Append("WHERE FeedBackID=@FeedBackID "); return DbHelper.ExecuteNonQuery(CommandType.Text,strSql.ToString(),param); } 调用: feedBackBLL.UpdateFeedBackStatus(_feedBackID, 4, DBNull.Value); 或者feedBackBLL.UpdateFeedBackStatus(_feedBackID, 4,null);代码语言:javascript复制方法二: public int UpdateFeedBackStatus(int _feedBackID, int _status, DateTime? _RequestDateTime) { SqlParameter[] param = { new SqlParameter("@FeedBackID", _feedBackID), new SqlParameter("@FeedBackStatusID", _status), new SqlParameter("@RequestDateTime", _RequestDateTime) }; param[2].IsNullable = true; StringBuilder strSql = new StringBuilder(); strSql.Append("UPDATE dbo.FeedBack "); strSql.Append("SET FeedBackStatusID=@FeedBackStatusID,RequestDateTime=@RequestDateTime "); strSql.Append("WHERE FeedBackID=@FeedBackID "); return DbHelper.ExecuteNonQuery(CommandType.Text,strSql.ToString(),param); } 调用: feedBackBLL.UpdateFeedBackStatus(_feedBackID, 4,null);二、C#中往数据库插入空值的问题 代码语言:javascript复制在用C#往数据库里面插入记录的时候, 可能有的字段你不赋值,那么这个字段的值就为null, 如果按一般想法的话,这个值会被数据库接受, 然后在数 据表里面显示为NUll, 实际上这就牵扯到一个类型的问题, C#中的NUll于SQL中的null是不一样的, SQL中的null用C#表示出来就 是DBNull.Value, 所以在进行Insert的时候要注意的地方. Example: SqlCommand cmd=new SqlCommand("Insert into Student values(@StuName,@StuAge)" ,con); cmd.parameters.add("@StuName" ,stuname); cmd.parameters.add("@StuAge" ,stuage); cmd.ExecuteNonQuery(); 这些代码看似没有问题, 其实当stuname于stuage中的任何一个值为null的时候, 这代码就会报错...汗!!! 解决办法: 其实最简单的办法就是进行判断, 当stuname或stuage为空时, 插入DBNull.Value. 但是这样当一个数据库有很多字段时或者是有很多张表时, 代码就会很多了,我也没有找到特别方便的方法,我的方法是:写一个静态的方法来对变量的值进行判断: Example : static public object SqlNull(object obj) { if (obj == null ) return DBNull.Value; return obj; } //用上面的方法对参数进行了判断 cmd.parameters.add("@StuName" ,SqlNull(stuname)); cmd.parameters.add("@StuAge" ,SqlNull(stuage)); cmd.ExecuteNonQuery(); |
CopyRight 2018-2019 实验室设备网 版权所有 |