SqlDataReader read()工作不合情理

时间:2017-05-23 06:47:29

标签: sql-server c#-4.0 sqldatareader

我正如您所见,从服务器获取数据(数据不为空)enter image description here

但不会输入while (reader.Read())

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

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

再次,数据不为空

编辑:添加了一些代码段

 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(...)我如何与第三方软件进行通信

3 个答案:

答案 0 :(得分:1)

第一个问题是您的SqlConnection为null。它将始终返回null值。

Secondally 您应该检查读者是否有像我使用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));
                        }
                      }
              }

答案 1 :(得分:1)

试试这个。

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

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

答案 2 :(得分:0)

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