生成系统资源的多个OleDbCommands超出

时间:2016-09-20 09:37:43

标签: c# oledbconnection oledbcommand

所以基本上我有一个C#项目,它遍历Microsoft Access 2016数据库中名为Student的表中的每个学生(300名学生)。在单个学生的单个迭代中,通过使用与Mathematics表具有一对一关系的ReadingStudent等其他表来获取属于该表的数据学生。

                try
            {
                OleDbCommand allStudents = new OleDbCommand("SELECT [NSN]"
                 + " FROM [Student]; ");

                allStudents.Connection = conn;
                OleDbDataAdapter allData = new OleDbDataAdapter(allStudents);
                DataTable allTable = new DataTable();
                allData.Fill(allTable);

                foreach (DataRow dr in allTable.Rows)
                {
                    string NSN = dr["NSN"].ToString();

                    OleDbCommand cmd = new OleDbCommand("SELECT * "
                        + "FROM (((([Student] s "
                        + "INNER JOIN [Student Extra] se ON se.[NSN] = s.[NSN]) "
                        + "INNER JOIN [Reading] r ON r.[NSN] = s.[NSN])"
                        + "INNER JOIN [Writing] w ON w.[NSN] = s.[NSN])"
                        + "INNER JOIN [Mathematics] m ON m.[NSN] = s.[NSN]) "

                        + "WHERE s.[NSN] = '" + NSN + "'; ");
                    cmd.Connection = conn;
                    OleDbDataAdapter daa = new OleDbDataAdapter(cmd);
                    DataTable dtt = new DataTable();
                    daa.Fill(dtt);


                    foreach (DataRow drr in dtt.Rows)
                    {
                        firstName = drr["Preferred Name"].ToString();
                        gender = drr["Gender"].ToString();
                        room = drr["Room Number"].ToString();
                        NSAchieve = drr["National Standard Achieve"].ToString();
                        NSProgress = drr["National Standard Progress"].ToString();

上面的代码只是我所拥有的代码片段,但这基本上就是函数启动的地方。

通过使用这些数据,我希望能够对其他表进行多个SELECT语句并比较它们并生成计算值。

Dictionary<string, OleDbCommand> d = new Dictionary<string, OleDbCommand>(); 
cmd = new OleDbCommand("SELECT [Achievement Statement]"
        + " FROM [National Standard Codes]"
        + " WHERE [National Standard Code] = '" + readingNSAchievementCode + "'; ");
        d["readingNSAchievementOTJ"] = cmd;

        cmd = new OleDbCommand("SELECT [" + NSAchieve + "]"
       + " FROM [Reading National Standards]"
       + " WHERE [Assessment] = '" + readingFinalAssessment + "'; ");
        d["readingNSAchievementComp"] = cmd;

        cmd = new OleDbCommand("SELECT [Timeframe]"
        + " FROM [Reading Statements]"
        + " WHERE [Year Code] = '" + NSProgress + "'; ");
        d["readingNSProgressTimeframe"] = cmd;

还有几个命令,(大约<150)。我使用Dictionary来存储我的命令,然后在FOREACH循环中执行命令。

foreach(KeyValuePair<string, OleDbCommand> pair in d)
        {
            try
            {
                string v = pair.Key;
                OleDbCommand dbCmd = pair.Value;


                dbCmd.Connection = conn;
                OleDbDataReader reader = dbCmd.ExecuteReader();
                reader.Read();
                readingDict[v] = reader.GetString(0);

            }
            catch (Exception e)
            {
                MessageBox.Show("Error at " + pair.Key + "\n\n Here is message " + e);
            }
        }

执行并获取我的值后,我想将我的数据存储到另一个名为Calculated的表中。

            string insert1 = "INSERT INTO [Calculated] (";
        int i = 0;
        Dictionary<string, string> dict = createDictionary(NSN);
        int len = dict.Count / 2;
        foreach (KeyValuePair<string, string> pair in dict)
        {
            string field = pair.Key;
            string value = pair.Value;

            if (i == (len - 1))
            {
                insert1 += "[" + field + "])";
                break;
            }
            else
            {
                insert1 += "[" + field + "], ";
            }

            i++;
        }
        insert1 += " VALUES (";
        i = 0;
        foreach (KeyValuePair<string, string> pair in dict)
        {
            string field = pair.Key;
            string value = pair.Value;

            if (i == len - 1)
            {
                insert1 += "'" +  value + "')";
                break;
            }
            else
            {
                insert1 += "'" + value + "', ";
            }

            i++;
        }

我构建了INSERT INTO查询,然后使用OleDbCommand执行。这需要重复300次,但出于开发目的,目前我的Student表中只有5名学生。但是,当在第4位学生之后执行时,它始终会始终在特定System Resources Exceeded给我一个错误OleDbCommand。我已经分别测试了每个命令,因此写OleDbCommand的方式没有问题。

我在这里尝试搜索,并尝试使用usingusing (OleDbConnection conn = new OleDbConnection(connectionStr))语句中包含第一个代码段,但由于我仍然是C#的新手,我无法制作解决方案。

0 个答案:

没有答案