【问题标题】:SqlDataReader read() working unconscionabilitySqlDataReader read() 工作不合情理
【发布时间】:2017-05-23 06:47:28
【问题描述】:

我按预期从服务器获取数据,如您在此处看到的(数据不为空)

但它不会进入while (reader.Read())

或者有时在一两次迭代后我得到异常

"Data is Null. This method or property cannot be called on Null values."

再次,数据不为空

编辑:添加了一些代码 sn-p

 System.Data.SqlClient.SqlConnection sqlConnection = null;
            try
            {
                sqlConnection = connectToDB();

                SqlCommand cmd = new SqlCommand(@"SELECT TOP 50 R.[RoundID] ,[DailyOrder] ,min(R.[RoundName]) as RoundName, min([EquipCode]) as TruckCode,
                                                sum(RD.[Weight]) as [Weight], RD.BlilCode, min(Blil.BlilName) as BlilName
                                                FROM [CVfeedDB].[dbo].[Planning.Rounds] as R 
                                                left join [CVfeedDB].[dbo].[Planning.RoundsDetail] as RD on R.RoundID = RD.RoundID 
                                                left join [CVfeedDB].[dbo].[constants.Blil] as Blil on RD.BlilCode = Blil.BlilCode  
                                                WHERE R.[ActionDate] = @ActionDate
                                                Group by R.[RoundID] ,[DailyOrder], RD.BlilCode  
                                                order by [DailyOrder]  ", sqlConnection);
                cmd.CommandType = System.Data.CommandType.Text;
                //string date = Convert.ToString();
                var dt = DateTime.ParseExact(Variables().Item("Date Select").get_Value(0).ToString(), "dd/MM/yyyy", null);
                var dt1 = dt.Date.ToString("yyyy-MM-dd");
                cmd.Parameters.AddWithValue("@ActionDate",dt1 );
                string prefix = "";
                int i = 1;
                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    prefix = ordersName + i.ToString() + "]";
                    List<object> list = new List<object>();
                    List<object> IDList = new List<object>();
                    int id = Convert.ToInt32(Variables().Item(prefix + ".LoopID").get_Value(0));
                    int k = reader.GetInt32(0);

                    if (id == 0 || id == k)//if no id or we are writing the same id                        
                    {
                        Variables().Item(prefix + ".LoopID").set_Value(0, reader.GetInt32(0));
                        //Variables().Item(prefix + ".").set_Value(0, reader.GetInt32(1));      //Order sequence
                        Variables().Item(prefix + ".LoopName").set_Value(0, reader.GetString(2));
                        Variables().Item(prefix + ".Truck").set_Value(0, Convert.ToInt32(reader.GetString(3)));
                        Variables().Item(prefix + ".Weight").set_Value(0, Convert.ToInt32(reader.GetDecimal(4)));
                        Variables().Item(prefix + ".MixID").set_Value(0, Convert.ToInt32(reader.GetString(5)));
                        Variables().Item(prefix + ".MixName").set_Value(0, reader.GetString(6));
                    }

请不要说 Variables().Item(...) 我如何与 3rd 方软件通信

【问题讨论】:

  • 你尝试了什么代码?显示您的代码示例。
  • @InnovaITveSolutions 添加了一些代码 sn-p
  • 您正在执行LEFT JOINs,然后直接从这些表中返回一些列,甚至不考虑聚合。您必须预计,在某些行中,某些值将为NULL,但您的代码目前无法避免这种情况。
  • @Damien_The_Unbeliever 正如我所说,我检查了数据库中的结果,结果中没有任何空值
  • 显然,ADO.NET 不同意你的观点。它不会将错误消息字符串随机分配给异常。它正在读取NULL。这里没有足够的上下文供任何人 else 调试这种情况。您是唯一拥有一整套可运行代码和实际数据的人。就目前情况而言,我们无能为力。如果可以,请创建一个minimal reproducible example。

标签: sql-server c#-4.0 sqldatareader


【解决方案1】:

第一个问题是您的 SqlConnection 为空。它总是返回空值。

其次 您应该使用 reader.Hasrows 检查阅读器是否有像我这样的行

System.Data.SqlClient.SqlConnection sqlConnection = null;
                try
                {
                    sqlConnection = connectToDB();

                    SqlCommand cmd = new SqlCommand(@"SELECT TOP 50 R.[RoundID] ,[DailyOrder] ,min(R.[RoundName]) as RoundName, min([EquipCode]) as TruckCode,
                                                    sum(RD.[Weight]) as [Weight], RD.BlilCode, min(Blil.BlilName) as BlilName
                                                    FROM [CVfeedDB].[dbo].[Planning.Rounds] as R 
                                                    left join [CVfeedDB].[dbo].[Planning.RoundsDetail] as RD on R.RoundID = RD.RoundID 
                                                    left join [CVfeedDB].[dbo].[constants.Blil] as Blil on RD.BlilCode = Blil.BlilCode  
                                                    WHERE R.[ActionDate] = @ActionDate
                                                    Group by R.[RoundID] ,[DailyOrder], RD.BlilCode  
                                                    order by [DailyOrder]  ", sqlConnection);
                    cmd.CommandType = System.Data.CommandType.Text;
                    //string date = Convert.ToString();
                    var dt = DateTime.ParseExact(Variables().Item("Date Select").get_Value(0).ToString(), "dd/MM/yyyy", null);
                    var dt1 = dt.Date.ToString("yyyy-MM-dd");
                    cmd.Parameters.AddWithValue("@ActionDate",dt1 );
                    string prefix = "";
                    int i = 1;
                    SqlDataReader reader = cmd.ExecuteReader();

                if(reader.HasRows)
                {
                    while (reader.Read())
                    {
                        prefix = ordersName + i.ToString() + "]";
                        List<object> list = new List<object>();
                        List<object> IDList = new List<object>();
                        int id = Convert.ToInt32(Variables().Item(prefix + ".LoopID").get_Value(0));
                        int k = reader.GetInt32(0);

                        if (id == 0 || id == k)//if no id or we are writing the same id                        
                        {
                            Variables().Item(prefix + ".LoopID").set_Value(0, reader.GetInt32(0));
                            //Variables().Item(prefix + ".").set_Value(0, reader.GetInt32(1));      //Order sequence
                            Variables().Item(prefix + ".LoopName").set_Value(0, reader.GetString(2));
                            Variables().Item(prefix + ".Truck").set_Value(0, Convert.ToInt32(reader.GetString(3)));
                            Variables().Item(prefix + ".Weight").set_Value(0, Convert.ToInt32(reader.GetDecimal(4)));
                            Variables().Item(prefix + ".MixID").set_Value(0, Convert.ToInt32(reader.GetString(5)));
                            Variables().Item(prefix + ".MixName").set_Value(0, reader.GetString(6));
                        }
                      }
              }

【讨论】:

  • 第一。在connectToDB(); 函数中,我连接到数据库(并且我检查了状态是否打开)。秒。 reader.HasRows 总是通过。第三:正如我所说,我检查了阅读器,它具有价值
【解决方案2】:

试试这个。

string namethestore = myReader.IsDBNull(namePos) 
                                          ? string.Empty 
                                          : reader.GetString(reader.GetString(2));

 Variables().Item(prefix + ".LoopName").set_Value(0, namethestore);

【讨论】:

    【解决方案3】:

    我已将isNull(rowname, 0 ) 添加到有问题的行中,它解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 2011-11-13
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多