我正在开展一个工作项目,并且遇到了Connection must be valid and open
错误。嵌套for
循环期间出现错误。它查看每个商店编号,并使用该编号作为参数从数据库中检索信息。
我遇到的问题是第一个号码传递后代码错误输出。我已尝试单独插入商店代码,以了解是否" 02"是唯一有效的,我发现40范围内的所有数字都有效。
我检查了连接的状态,它告诉我每次尝试运行时它都是打开的。我已经读过这应该意味着它是SQL的错误。我和老板检查了一下,然后他看了看,告诉我SQL没用。
我非常希望有人可以帮助我。我提供了存储编号所在的数组,嵌套的for循环,我们创建SQL的代码,以及SQL传递给下面数据库的代码。提前感谢任何帮助!!
商店编号的字符串数组
String[] stores = new String[] { "02", "03", "05", "08", "11", "15", "30", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "06" };
嵌套' for'环
for (int questionsCycle = 0; questionsCycle < questions.Length; questionsCycle++)
{
for (int storeCycle = 0; storeCycle < stores.Length; storeCycle++)
{
DataTable dbTable4 = getSurveys.GetCustomerEmailSurveyResultsForFlagStoreAndDates("C", stores[storeCycle], DateTime.Now.AddDays(-1).ToShortDateString(), DateTime.Now.AddDays(-1).ToShortDateString()); //The method that gives me the error
for (int surveysCycle = 0; surveysCycle < dbTable4.Rows.Count; surveysCycle++)
{
DataRow dbRow4 = dbTable4.Rows[surveysCycle];
runningTotal += Convert.ToInt16(dbRow4[questions[questionsCycle]].ToString());
dbRow4 = null;
}
if (runningTotal != 0)
{
data[questionsCycle, storeCycle] = dbTable4.Rows.Count.ToString() + "/" + ((double)runningTotal / dbTable4.Rows.Count).ToString("#0.00");
questionTotals[questionsCycle, storeCycle] = (double)runningTotal / dbTable4.Rows.Count;
questionRows[questionsCycle, storeCycle] = dbTable4.Rows.Count;
}
companyTotal += runningTotal;
surveyTotal += dbTable4.Rows.Count;
runningTotal = 0;
dbTable4 = null;
}
在我们的数据管理器中创建SQL的方法:
public DataTable GetCustomerEmailSurveyResultsForFlagStoreAndDates(string flag, string strStore, string strBegDate, string strEndDate)
{
string strSQL = "select * from customer_email_survey";
strSQL += " where sent_date between '" + strBegDate + "' and '" + strEndDate + "'";
//added by cjc on 5/31/13...sales leaders change for customer experiecne results
if (flag == "Experience")
{
strSQL += " and (flag = 'D' OR flag = 'P')";
}
else
{
strSQL += " and flag = '" + flag + "'";
}
if (strStore == "MATT")
{
strStore = "41', '42', '43', '44', '46', '47', '48', '49', '61";
}
strSQL += " and store_code in ('" + strStore + "'";
switch (strStore)
{
case "11":
strSQL += ", '12')";
break;
case "15":
strSQL += ", '16')";
break;
case "08":
strSQL += ", '22')";
break;
case "30":
strSQL += ", '31')";
break;
default:
strSQL += ")";
break;
}
DataTable dbTable = this.GetData(strSQL);
return dbTable;
}
连接数据库并传递sql
的方法private DataTable GetData(string strSQL)
{
MySqlConnection dbConn = new MySqlConnection(strConnectionString);
MySqlDataAdapter dbAdapter = new MySqlDataAdapter(strSQL,dbConn);
DataSet dbSet = new DataSet();
dbAdapter.Fill(dbSet); //The line where the code bombs
DataTable dbTable = new DataTable();
dbTable = dbSet.Tables[0];
dbAdapter = null;
dbSet = null;
dbConn.Close();
return dbTable;
}
这是我从堆栈跟踪中得到的(如果我做得正确的话):
SendCustomerSurveyResults位于文件中的偏移3802处:行:列C:\ Dev \ legacy \ CustomerEmail \ CustomerEmailProgram.cs:31175:25
CustomerEmailProgram_Load位于文件中的偏移量120处:行:列C:\ Dev \ legacy \ CustomerEmail \ CustomerEmailProgram.cs:68:17
OnLoad在文件中的偏移量373处:行:列:0:0
CreateControl位于文件中的偏移434处:line:column:0:0
在文件中的偏移36处的CreateControl:line:column:0:0
WmShowWindow在文件中的偏移221处:行:列:0:0
WndProc在文件中的偏移量1087处:行:列:0:0
WndProc在文件中偏移70处:行:列:0:0
在文件中偏移量为173的DebuggableCallback:line:column:0:0
SetVisibleCore在文件中的偏移334处:行:列:0:0
SetVisibleCore在文件中的偏移384处:行:列:0:0
RunMessageLoopInner位于文件中的偏移量491处:line:column:0:0
RunMessageLoop位于文件中的偏移量101处:行:列:0:0
主要位于文件中的偏移67:行:列C:\ Dev \ legacy \ CustomerEmail \ CustomerEmailProgram.cs:632:13
文件中偏移0处的_nExecuteAssembly:line:column:0:0
文件中偏移71处的RunUsersAssembly:line:column:0:0
在文件中的偏移155处运行:line:column:0:0
ThreadStart位于文件中的偏移量77处:行:列:0:0