动软版本问题 +修改时,参数化查询时,提示少了某些参数, 但在数据库中这些参数为空。

一哥们。用动软2.6.7来写ADD和Update();

数据库中字段可以为空。后台赋值为“”;结果修改时,首先给MODEL赋值,发现怎么成了NULL了。

而数据库终中也不是“NULL”,证明数据库中是空串。

于是发现动软生成的三层里面有一个SQLSERVERDAL。里面的UPDATE时,多了一个IF判断。

我们的都是

 if(ds.Tables[0].Rows[0]["beginTime"].ToString()!="")
    {
     model.beginTime=DateTime.Parse(ds.Tables[0].Rows[0]["beginTime"].ToString());
    }

他的是

if(ds.Tables[0].Rows[0]["beginTime"]!=null&&ds.Tables[0].Rows[0]["beginTime"].ToString()!=""){}

于是乎,导致一直取不到值。赋值也有null值。更不要说是UPDATE操作了。

但是注意:并不是说这样写是错的。而是看你平时针对数据库为空的情况在程序后台中怎么添加,怎么修改的习惯。

  LOCATION中

 nvarchar  Memo 是可以为空的。model.BirdsNum = ds.Tables[0].Rows[0]["BirdsNum"].ToString();

其他的有的检查ds.Tables[0].Rows[0]["ServiceType"].ToString() != ""

有的还坚持ds.Tables[0].Rows[0]["beginTime"]!=null

各种动软版本都不同。导致你的可空字段的值很不稳定。

所以

     if (ds.Tables[0].Rows.Count > 0)
            {
                model.DeviceId = ds.Tables[0].Rows[0]["DeviceId"].ToString();
                if (ds.Tables[0].Rows[0]["ServiceType"].ToString() != "")
                {
                    model.ServiceType = int.Parse(ds.Tables[0].Rows[0]["ServiceType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Longtitude"].ToString() != "")
                {
                    model.Longtitude = decimal.Parse(ds.Tables[0].Rows[0]["Longtitude"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Latitude"].ToString() != "")
                {
                    model.Latitude = decimal.Parse(ds.Tables[0].Rows[0]["Latitude"].ToString());
                }
                model.Direction = ds.Tables[0].Rows[0]["Direction"].ToString();
                if (ds.Tables[0].Rows[0]["Velocity"].ToString() != "")
                {
                    model.Velocity = decimal.Parse(ds.Tables[0].Rows[0]["Velocity"].ToString());
                }
                model.BirdsOrientation = ds.Tables[0].Rows[0]["BirdsOrientation"].ToString();
                model.BirdsFlyDirection = ds.Tables[0].Rows[0]["BirdsFlyDirection"].ToString();
                model.BirdsNum = ds.Tables[0].Rows[0]["BirdsNum"].ToString();
                model.BirdsSize = ds.Tables[0].Rows[0]["BirdsSize"].ToString();
                model.BirdsHeight = ds.Tables[0].Rows[0]["BirdsHeight"].ToString();
                model.BirdsAction = ds.Tables[0].Rows[0]["BirdsAction"].ToString();
                model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                if (ds.Tables[0].Rows[0]["LocationTime"].ToString() != "")
                {
                    model.LocationTime = DateTime.Parse(ds.Tables[0].Rows[0]["LocationTime"].ToString());
                }
                model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                return model;
            }
            else
            {
                return null;
            }

综上所述,解决方案分两个方面

 

1. 养成自己的编码习惯,看看修改和赋值时,自己想赋值成什么样。

2.不要批量生成。每一个代码文件,特别是SQLServerDAL都要看看,是否和自己的编码习惯一直。不一致再改动。

 

反正有句话我在心里说:动软这点。正烦人。但是动软也不能适应所有的需求啊。而且动软是别人的定义。所以还是把这些修改成自己习惯的吧。哈哈。谁让我也是程序员那。

 

————————下面是一种情况

参数化查询时,提示少了某些参数, 但在数据库中这些参数为空。

而且还是在修改时发生的。

我一直以为是三层中的“可空符号?”没写的问题。后台我加上此符号,发现string? aa这样报错。

原来。string字符串本身就是个引用类型。可以是NULL 。不需要在加“?”。一般是INT 或者 DECIMAL 或者 DATETIME 这样的类型加“?”;

那是什么错误那?

后来我又一想,是修改。就需要先得到MODEL.直接拿一个MODEL赋值。然后UPDATE,当然有问题,因为不是ADD。是update。必须先赋值才可以啊。

于是我才明白。原来是在修改时,没有 MODEL=BLL.GETMODEL(ID);

已经赋了model的 提示MEMO字段缺失。一看原来是在参数化查询时,NULL字段没办法表示。一般更新就写个判断。看看MODEL是否只NULL。是NULL就赋值为"";

反正经过动软三层的SQLServerDAL.也会变成数据库中是NULL。不是NULL的时候就再赋一遍原来的值喽(其实我还是建议改动软三层中的代码)。

还是要看自己的习惯啊。

本人声明: 个人主页:沐海(http://www.cnblogs.com/mahaisong) 以上文章都是经过本人设计实践和阅读其他文档得出。如果需要探讨或指教可以留言或加我QQ!欢迎交流!
原文地址:https://www.cnblogs.com/mahaisong/p/2249855.html