public static void LoadSecondaryTable(string component, string secondaryPathAndFileName, ref List <string> warnings) { //Create new item in SecondaryGroups structure (if it is not //already there) with data from secondaryPathAndFileName. if (File.Exists(secondaryPathAndFileName)) { if (SecondaryGroups.ContainsKey(secondaryPathAndFileName)) { SecondaryGroups.Remove(secondaryPathAndFileName); //NOTE: the above comment is not valid. Keep it until debugging!!!!!: //Do not load the data from this file since it is already in memory. //In memory it could have been modified but we want to keep it as it //is, just as Word and Excel does when a file is loaded again. } StreamReader secondaryFile; secondaryFile = File.OpenText(secondaryPathAndFileName); try { var thisGroup = new SecondaryGroup(); thisGroup.Component = component; thisGroup.FileName = secondaryPathAndFileName; thisGroup.NPoints = Convert.ToInt64(secondaryFile.ReadLine()); var thisTable = new SecondaryTable[thisGroup.NPoints]; for (int j = 0; j <= thisGroup.NPoints - 1; ++j) { string next = secondaryFile.ReadLine(); string[] splt = next.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); int nColumns = splt.Length; switch (nColumns) { case 1: thisTable[j].Col0 = splt[0]; break; case 2: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; break; case 3: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; thisTable[j].Col2 = splt[2]; break; case 4: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; thisTable[j].Col2 = splt[2]; thisTable[j].Col3 = splt[3]; break; case 5: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; thisTable[j].Col2 = splt[2]; thisTable[j].Col3 = splt[3]; thisTable[j].Col4 = splt[4]; break; case 6: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; thisTable[j].Col2 = splt[2]; thisTable[j].Col3 = splt[3]; thisTable[j].Col4 = splt[4]; thisTable[j].Col5 = splt[5]; break; case 7: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; thisTable[j].Col2 = splt[2]; thisTable[j].Col3 = splt[3]; thisTable[j].Col4 = splt[4]; thisTable[j].Col5 = splt[5]; thisTable[j].Col6 = splt[6]; break; case 8: thisTable[j].Col0 = splt[0]; thisTable[j].Col1 = splt[1]; thisTable[j].Col2 = splt[2]; thisTable[j].Col3 = splt[3]; thisTable[j].Col4 = splt[4]; thisTable[j].Col5 = splt[5]; thisTable[j].Col6 = splt[6]; thisTable[j].Col7 = splt[7]; break; } } secondaryFile.Close(); //Store bridge profile table: thisGroup.Table = thisTable; //Store this new group: SecondaryGroups.Add(secondaryPathAndFileName, thisGroup); } catch (Exception ex) { secondaryFile.Close(); MessageBox.Show(Idioma("ERROR 0605171628: error reading file ", "ERROR 0605171628: error leyendo archivo ") + "\n\n" + secondaryPathAndFileName + "\n\n" + ex.Message, "RiverFlow2D", MessageBoxButtons.OK, MessageBoxIcon.Error); //Create a Default empty file. CreateSecondaryDefaultFile(component, secondaryPathAndFileName, 5); if (warnings.IndexOf(secondaryPathAndFileName) == -1) { warnings.Add(secondaryPathAndFileName); } } } else { //secondaryPathAndFileName doesn't exist. Create a Default empty file. CreateSecondaryDefaultFile(component, secondaryPathAndFileName, 5); if (warnings.IndexOf(secondaryPathAndFileName) == -1) { warnings.Add(secondaryPathAndFileName); } } }
protected override void OK() { MongoCollection <BsonDocument> coll = _db.GetCollection(PrimaryTable); string fk = GetFKColumnName(); string foreigntablepk = getpk(SecondaryTable); string pk = GetPKColumnName(); string stname = SecondaryTable.Substring(SecondaryTable.IndexOf(".") + 1); string ptpk = getpk(PrimaryTable); using (var sconn = new SqlConnection(ConnectionString)) using (var pconn = new SqlConnection(ConnectionString)) { sconn.Open(); pconn.Open(); using (SqlCommand scmd = sconn.CreateCommand()) using (SqlCommand pcmd = pconn.CreateCommand()) { const string cmdtext = "select * from {0} where {1} is not null order by {1}"; pcmd.CommandText = string.Format(cmdtext, PrimaryTable, pk); scmd.CommandText = string.Format(cmdtext, SecondaryTable, fk); using (SqlDataReader pr = pcmd.ExecuteReader()) using (SqlDataReader sr = scmd.ExecuteReader()) { var dic = new SortedDictionary <object, List <BsonDocument> >(); while (sr.Read()) { BsonDocument doc = GetDocuementFromRecord(sr, foreigntablepk); if (!SaveSecondaryKeys) { doc.Remove(fk); } object key = sr[fk]; if (dic.ContainsKey(key)) { dic[key].Add(doc); } else { dic.Add(key, new List <BsonDocument> { doc }); } } while (pr.Read()) { BsonDocument doc = GetDocuementFromRecord(pr, ptpk); object key = pr[pk]; try { var arr = new BsonArray(dic[key]); doc.Add(stname, arr); } catch (KeyNotFoundException e) { //Console.WriteLine(e); } coll.Save(doc); } } } } }
public static void CreateSecondaryDefaultFile(string component, string fileName, int nValues) { var aOneLineTable = new SecondaryTable[1]; string aSingleLine = ""; switch (nValues) { case 1: aOneLineTable[0].Col0 = "0"; aSingleLine = "0"; break; case 2: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aSingleLine = "0 0"; break; case 3: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aOneLineTable[0].Col2 = "0"; aSingleLine = "0 0 0"; break; case 4: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aOneLineTable[0].Col2 = "0"; aOneLineTable[0].Col3 = "0"; aSingleLine = "0 0 0 0"; break; case 5: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aOneLineTable[0].Col2 = "0"; aOneLineTable[0].Col3 = "0"; aOneLineTable[0].Col4 = "0"; aSingleLine = "0 0 0 0 0"; break; case 6: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aOneLineTable[0].Col2 = "0"; aOneLineTable[0].Col3 = "0"; aOneLineTable[0].Col4 = "0"; aOneLineTable[0].Col5 = "0"; aSingleLine = "0 0 0 0 0 0"; break; case 7: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aOneLineTable[0].Col2 = "0"; aOneLineTable[0].Col3 = "0"; aOneLineTable[0].Col4 = "0"; aOneLineTable[0].Col5 = "0"; aOneLineTable[0].Col6 = "0"; aSingleLine = "0 0 0 0 0 0 0"; break; case 8: aOneLineTable[0].Col0 = "0"; aOneLineTable[0].Col1 = "0"; aOneLineTable[0].Col2 = "0"; aOneLineTable[0].Col3 = "0"; aOneLineTable[0].Col4 = "0"; aOneLineTable[0].Col5 = "0"; aOneLineTable[0].Col6 = "0"; aOneLineTable[0].Col7 = "0"; aSingleLine = "0 0 0 0 0 0 0 0"; break; } var data = new SecondaryGroup(); data.Component = component; data.FileName = fileName; data.NPoints = 1; data.Table = aOneLineTable; SecondaryGroups.Add(fileName, data); // Write data. TextWriter file = new StreamWriter(fileName); file.WriteLine("1"); file.WriteLine(aSingleLine); file.Close(); }