【ITOO】---評教題庫匯入
阿新 • • 發佈:2019-02-18
今天上午做匯入,開始還特別的順利,可以一次性匯入許多資料,可是到了下午,我新增一個判斷:“判斷資料庫中是否已經存在已經要匯入的資料”,由於是excel種的每一條都得判斷,所以我就圖省事把“查庫-判斷是否存在”的步驟程式碼寫在了for迴圈裡面!結果就報了這麼一個奇葩錯誤:“Multiple simultaneous connections or connections with different
connection strings inside the same transaction are not currently supported.”
這個錯誤翻譯過來就是:“ 目前不支援多個併發連線或連線不同的連線字串在相同事務中”!
總結:
這個錯誤翻譯過來就是:“ 目前不支援多個併發連線或連線不同的連線字串在相同事務中”!
一開始各種不明白,弄了半天的資料庫連線字串,還非常奇怪:我就連了一次啊!!!後來馮堯給了一個思路就是:判斷資料庫中是否存在用到了EF連線了一次資料庫,批量新增資料又用到了一次資料庫,而且又寫到了一個迴圈裡面,這可能就是所謂的在一次事務當中連線了多次資料庫吧!你試試把判斷是否重複的程式碼拿出來!
就這樣,我就開始了提取程式碼,最終程式碼形成如下,僅作參考://得到匯入目標表的DataTable Dictionary<int, DataTable> dicTargetTable = this.GetImportTable(strPath, strXMLName, dicDefaultColumn, strDBKey); //得到匯入第三張表的DataTable Dictionary<int, DataTable> dicThirdTable = this.GetThirdTable(); //得到過程中出現的問題表 Dictionary<int, DataTable> dicErrorTable = this.GetErrorTable(); //資料庫連線字串 //獲得配置檔案連結資料庫的字串 //string connstr = ConfigurationManager.AppSettings["connectionString"]; ITOO.Library.Core.Common.MySqlHelper mysql = new ITOO.Library.Core.Common.MySqlHelper(strDBKey); //先吧資料庫中存在的資料取出來放到一個list裡面 List<EvaluationQuestionBank> listNew = new List<EvaluationQuestionBank>(); for (int intTableIndex = 0; intTableIndex < dicTargetTable.Count; intTableIndex++) { if (dicTargetTable[intTableIndex].Rows.Count > 0) { DataTable dtTarget = dicTargetTable[intTableIndex]; for (int i = 0; i < dtTarget.Rows.Count; i++) { EvaluationQuestionBank questionbank = new EvaluationQuestionBank(); // DataTable singledtTarget = new DataTable(); string questioncontent = dtTarget.Rows[i]["QuestionContent"].ToString(); string paperid = dicDefaultColumn["PaperID"].ToString(); //查詢該考試內容和paperid下是否有重複的內容,有則不用匯入 Boolean isexist = isExistPaperIdAndQuestionContent(paperid, questioncontent); if (isexist == false) { //放入到新的list中 questionbank.PaperId = dicDefaultColumn["PaperID"].ToString(); questionbank.QuestionContent = dtTarget.Rows[i]["QuestionContent"].ToString(); questionbank.QuestionNo =Convert.ToInt16( dtTarget.Rows[i]["QuestionNo"]); questionbank.QuestionScore= Convert.ToDecimal(dtTarget.Rows[i]["QuestionScore"].ToString()); questionbank.TableName = dtTarget.TableName.ToString(); } listNew.Add(questionbank); } } } //然後對這個新的list提取資訊 try { using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { for (int intTableIndex = 0; intTableIndex < dicTargetTable.Count; intTableIndex++) { if (dicTargetTable[intTableIndex].Rows.Count > 0) { //DataTable dtTarget = dicTargetTable[intTableIndex]; //開始不同 StringBuilder sbSql = new StringBuilder(); for (int i = 0; i < listNew.Count; i++) { string QuestionId = System.Guid.NewGuid().ToString(); DateTime TimeStamp = DateTime.Now; string IsDeleteQuestion = "1"; string QuestionOrderId = "0"; if (listNew[i].QuestionNo == 0) { continue; } //sql語句拼接 sbSql.Append("insert into ").Append(listNew[i].TableName.ToString()).Append(" (QuestionNo,QuestionContent,QuestionScore,QuestionId,IsDeleteQuestion,QuestionOrderId,TimeStamp,PaperId) values ("); sbSql.Append("'" + listNew[i].QuestionNo+ "',") .Append("'" + listNew[i].QuestionContent + "',") .Append("'" + listNew[i].QuestionScore + "',") .Append("'" + QuestionId + "',") .Append("'" + IsDeleteQuestion + "',") .Append("'" + QuestionOrderId + "',") .Append("'" + TimeStamp + "',") .Append("'" + dicDefaultColumn["PaperID"] + "'") .Append(");"); } if (sbSql.Length==0) { break; } //往SQLHelper裡面提交資料 int flag = mysql.ExecuteNonQuery(sbSql.ToString()); } } scope.Complete(); } } catch (Exception e) { throw new Exception(e.Message); } Boolean bolIsExistErrorData = false; foreach (int intErrorTableIndex in dicErrorTable.Keys) { if (dicErrorTable[intErrorTableIndex].Rows.Count > 0) { bolIsExistErrorData = true; } } if (bolIsExistErrorData) { return dicErrorTable; } return null;
總結:
ITOO5.0用到了myql資料庫,很多問題都是和sqlserver不一樣的!而且mysql的修改和新增都是有限制的,只能有一個程式對同一個表進行修改和新增,所以就會造成很多問題,平時要小心,仔細,不能偷懶啊!