【发布时间】: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