好吧,我正在ASP平台上准备一个数据输入应用程序netcore2.1,这是我的第一个应用程序,因此我像laravel项目一样处理它,因此几乎没有Razor格式可以为我节省很多时间。时间,我明白了。 我面临的问题是有一个名为 getAllData 的方法,该方法在数据库中飞行并获取数据以编辑约60个框。我反复运行它,因为遇到了许多较小的错误,并且在重新运行了几次之后,结果才停止出现。断点显示至少有一些数据正在准备设置为List,但从未在那里建立过。我使用嵌套的数据读取器来获取一个长列表。现在它只会运行一次,不再被检测到。没有错误提示。我对为什么需要帮助感到困惑。
来自JS的控制器动作,从控制器运行时也是如此...
var theId = $("#ProjectId").val();
var theId2 = $("#InvestorId").val();
$.ajax({
url: '/Home/getAllData/',
dataType: "json",
type: "GET",
data: { id: theId, id2: theId2 },
contentType: 'application/json',
async: true,
processData: true,
cache: false,
success: function (response) {
response = JSON.parse(JSON.stringify(response));
if (response[0] == null) {
alertify.error("Nothing is Coming In!! possibly too many tries at setting its values");
} else {
//LOAD THE DATA TO PAGE ELEMENTS
}
它不断触发alertify,并且console.log显示一个空数组...消息反映了我的经验。
public List<AllData> getAllData(int id, int id2)
{
SqlConnection con = new SqlConnection(GetConStringTemp.ConString());
string query = "Select --- where ProjectId=" + id + "";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
var temp = new List<AllData>();
var plannedProjectOutput = new List<PlannedServiceViewModel>();
var actualProjectOutput = new List<ActualServiceViewModel>();
var investorAddress = new List<InvestorAddressViewModel>();
//a bunch more vars...
var queryOutput = "Select --- where ProjectId=" + id + "";
var cmdOutput = new SqlCommand(queryOutput, con);
var readerOutput = cmdOutput.ExecuteReader();
while (readerOutput.Read())
{
if (readerOutput.GetBoolean(1) == true)
{
actualProjectOutput.Add(new ActualServiceViewModel
{
TypeOfProduct = readerOutput.IsDBNull(0) ? "" : readerOutput.GetString(0),
ActualQuantity = readerOutput.IsDBNull(2) ? 0 : readerOutput.GetDecimal(2),
ActualUnits = readerOutput.IsDBNull(3) ? "" : readerOutput.GetString(3),
ActualDomestic = readerOutput.IsDBNull(4) ? 0 : readerOutput.GetDecimal(4),
ActualExport = readerOutput.IsDBNull(5) ? 0 : readerOutput.GetDecimal(5)
});
}
else
{
plannedProjectOutput.Add(new PlannedServiceViewModel
{
TypeOfProduct = readerOutput.IsDBNull(0) ? "" : readerOutput.GetString(0),
PlannedQuantity = readerOutput.IsDBNull(2) ? 0 : readerOutput.GetDecimal(2),
PlannedUnits = readerOutput.IsDBNull(3) ? "" : readerOutput.GetString(3),
PlannedDomestic = readerOutput.IsDBNull(4) ? 0 : readerOutput.GetDecimal(4),
PlannedExport = readerOutput.IsDBNull(5) ? 0 : readerOutput.GetDecimal(5)
});
}
}
readerOutput.Close();
// a bunch more definitions too...
while (reader.Read())
{
var cmd2 = new SqlCommand("Select --- from Investor where InvestorId=" + id2 + "", con);
SqlDataReader reader2 = cmd2.ExecuteReader();
while (reader2.Read())
{
if (reader2.GetString(0) == "4")
{
var queryCompany = "Select ---- from Investor where InvestorId=" + id2 + "";
var cmdCompany = new SqlCommand(queryCompany, con);
var readerCompany = cmdCompany.ExecuteReader();
while (readerCompany.Read())
{
var queryReq = "Select ---- where ProjectId=" + id + "";
var cmdReq = new SqlCommand(queryReq, con);
var readerReq = cmdReq.ExecuteReader();
while (readerReq.Read())
{
var queryCostPlanned = "Select ---- ProjectCost where ProjectId=" + id + " AND isActual=0";
var cmdCostPlanned = new SqlCommand(queryCostPlanned, con);
var readerCostPlanned = cmdCostPlanned.ExecuteReader();
while (readerCostPlanned.Read())
{
var queryCostActual = "Select --- from ProjectCost where ProjectId=" + id + " AND isActual=1";
var cmdCostActual = new SqlCommand(queryCostActual, con);
var readerCostActual = cmdCostActual.ExecuteReader();
while (readerCostActual.Read())
{
var queryEmployementPlanned = "Select --- where ProjectId=" + id + " AND isActual=0";
var cmdEmployementPlanned = new SqlCommand(queryEmployementPlanned, con);
var readerEmployementPlanned = cmdEmployementPlanned.ExecuteReader();
while (readerEmployementPlanned.Read())
{
var queryEmployementActual = "Select --- where ProjectId=" + id + " AND isActual=1";
var cmdEmployementActual = new SqlCommand(queryEmployementActual, con);
var readerEmployementActual = cmdEmployementActual.ExecuteReader();
while (readerEmployementActual.Read())
{
temp.Add(new AllData
{
LegalStatus = reader2.IsDBNull(0) ? "" : reader2.GetString(0),
FirstName = readerCompany.IsDBNull(1) ? "" : readerCompany.GetString(1),
FirstNameAmh = readerCompany.IsDBNull(0) ? "" : readerCompany.GetString(0),
TINNumber = readerCompany.IsDBNull(2) ? "" : readerCompany.GetString(2),
UserId = readerCompany.IsDBNull(3) ? "" : readerCompany.GetString(3),
TypeInvestor = readerCompany.IsDBNull(4) ? "" : readerCompany.GetString(4),
BranchPlace = readerCompany.IsDBNull(5) ? "" : readerCompany.GetString(5),
CountryOfOrigin = readerCompany.IsDBNull(6) ? "" : readerCompany.GetString(6),
ProjectName = reader.IsDBNull(1) ? "" : reader.GetString(1),
ProjectDescription = reader.IsDBNull(2) ? "" : reader.GetString(2),
FileNumber = reader.IsDBNull(19) ? "" : reader.GetString(19),
StartDate = reader.IsDBNull(3) ? "" : reader.GetString(3),
OperationDate = reader.IsDBNull(4) ? "" : reader.GetString(4)
// a bunch more assignments
});
}
readerEmployementActual.Close();
}
readerEmployementPlanned.Close();
}
readerCostActual.Close();
}
readerCostPlanned.Close();
}
readerReq.Close();
}
readerCompany.Close();
}
else
{
same with some difference....
}
reader.Close();
con.Close();
AllDatas = temp;
return AllDatas;
}
它很少,它通常可以工作,如果您尝试多次获得它,则不会,
谢谢
答案 0 :(得分:0)
我设法得到了结果集。我处理它的方式,当我使所有查询级联以得到一个统一的List时,我发现有一些空白或null返回,但是由于我将其编码为null,所以我不明白为什么它不起作用在这种特定情况下。虽然我仍然不知道为什么,但是我将查询从嵌套数据读取器分解为单个查询,并隔离了错误的查询并使其他查询正常工作。
经验法则,如果可以的话,不要级联数据读取器,我让它们在程序的其他部分工作,但这是最复杂的。它将为您节省很多头痛。