为什么我从数据库中收到索引越界错误
本文关键字:索引 越界 错误 数据库 为什么 | 更新日期: 2023-09-27 18:31:27
我知道索引越界是怎么回事。当我调试时,我也明白为什么。基本上,正在发生的事情是我对数据库进行过滤以查找潜在/待处理的记录。然后,我收集这些号码的数组,将它们发送到另一台服务器,以检查这些号码是否已升级为销售。如果已升级到销售,服务器将使用新的销售订单 ID 和我的旧待处理销售订单 ID (源 ID) 进行响应。然后,我对该列表执行一个 for 循环以过滤该特定 SourceID,并将 SourceID 更新为销售订单 ID,并更改其他几个值。问题是,当我在第一个过滤器上使用该过滤器时,它会抛出一个索引越界错误。我检查过滤器返回的结果,它说 0。我觉得这有点奇怪,因为我从列表中取了销售订单号,所以它应该在那里。所以我不知道这笔交易是什么。下面是引发错误的有问题的代码。而且它不会一直这样做。就像我今天早上刚刚运行代码一样,它没有抛出错误。但昨晚在我回家之前就发生了。
filter.RowFilter = string.Format("Stage = '{0}'", Potential.PotentialSale);
if (filter.Count > 0)
{
var Soids = new int[filter.Count];
Console.Write("Searching for Soids - (");
for (int i = 0; i < filter.Count; i++)
{
Console.Write(filter[i][1].ToString() + ",");
Soids[i] = (int)filter[i][1];
}
Console.WriteLine(")");
var pendingRecords = Server.GetSoldRecords(Soids);
var updateRecords = new NameValueCollection();
for (int i = 0; i < pendingRecords.Length; i++)
{
filter.RowFilter = "Soid = " + pendingRecords[i][1];
filter[0].Row["Soid"] = pendingRecords[i][0];
filter[0].Row["SourceId"] = pendingRecords[i][1];
filter[0].Row["Stage"] = Potential.ClosedWon;
var potentialXML = Potential.GetUpdatePotentialXML(filter[0].Row["Soid"].ToString(), filter[0].Row["Stage"].ToString());
updateRecords.Add(filter[0].Row["ZohoID"].ToString(), potentialXML);
}
如果我向右计数,第 17 行是抛出错误的错误。 pendingRecords 是一个 object[][] 数组。 pendingRecords[i] 是单个记录。 pendingRecords[i][0] 是新的销售订单 ID (SOID),pendingRecords[i][1] 是旧的 SOID(现在是源 ID)
对此有任何帮助吗? 是因为我要将 SOID 更改为新的 SOID,并且过滤器会自动更新吗?我只是不知道
好吧,我最终改变了它一起工作的方式,现在它实际上排序得更好一些。由于返回的表的结构,我将要发布的代码具有一堆硬编码数字。对此感到抱歉。从那以后,我学会了不要这样做,但是我现在正在从事一个不同的项目,当我必须更改程序时,我会改变它。但这是解决方案。
var potentials = Server.GetNewPotentials(); //loads all records from server
for (int i = 0; i < potentials.Length; i++)
{
var filter = AllPotentials.DefaultView;
var result1 = CheckSoidOrSource(potentials[i].Soid, true);
var result2 = CheckSoidOrSource(potentials[i].SourceID,false) ;
//This potential can't be found at all so let's add it to our table
if (result1+result2==0)
{
Logger.WriteLine("Found new record. Adding it to DataTable and sending it to Zoho");
AllPotentials.Add(potentials[i]);
filter.RowFilter = string.Format("Soid = '{0}'", potentials[i].SourceID);
var index = AllPotentials.Rows.IndexOf(filter[0].Row);
ZohoPoster posterInsert = new ZohoPoster(Zoho.Fields.Potentials, Zoho.Calls.insertRecords);
AllPotentials.Rows[index]["ZohoID"] = posterInsert.PostNewPotentialRecord(3, filter[0].Row);
}
//This potential is not found, but has a SourceId that matches a Soid of another record.
if (result1==0 && result2 == 1)
{
Logger.WriteLine("Found a record that needs to be updated on Zoho");
ZohoPoster posterUpdate = new ZohoPoster(Zoho.Fields.Potentials, Zoho.Calls.updateRecords);
filter.RowFilter = string.Format("Soid = '{0}'", potentials[i].SourceID);
var index = AllPotentials.Rows.IndexOf(filter[0].Row);
AllPotentials.Rows[index]["Soid"] = potentials[i].Soid;
AllPotentials.Rows[index]["SourceId"] = potentials[i].SourceID;
AllPotentials.Rows[index]["PotentialStage"] = potentials[i].PotentialStage;
AllPotentials.Rows[index]["UpdateRecord"] = true;
AllPotentials.Rows[index]["Amount"] = potentials[i].Amount;
AllPotentials.Rows[index]["ZohoID"] = posterUpdate.UpdatePotentialRecord(3, filter[0].Row);
}
}
AllPotentials.AcceptChanges();
}
private int CheckSoidOrSource(string Soid, bool checkSource)
{
var filter = AllPotentials.DefaultView;
if (checkSource)
filter.RowFilter = string.Format("Soid = '{0}' OR SourceId = '{1}'",Soid, Soid);
else
filter.RowFilter = string.Format("Soid = '{0}'", Soid);
return filter.Count;
}
基本上正在发生的事情是,当我以这种方式过滤数据时,我注意到了有关数据的一些信息。这两个结果只会返回以下结果 (0,0) (0,1) 和 (1,0) (0,0) 意味着该记录在此表中根本不存在,因此我需要添加它。(1,0) 表示销售订单 ID (Soid) 与表中的另一个 Soid 匹配,因此它已存在。最后 (0,1) 表示此表中不存在 Soid,但我找到了一条以 Soid 为源的记录......对我来说,这意味着将其作为来源的那个已经从潜在升级到销售,这反过来意味着我必须更新记录和 Zoho。 这对我来说大大减少了工作量,因为现在我不必搜索赢和输的记录,我只需要搜索丢失的记录。更少的代码相同的结果总是一件好事:)