I am trying to read a table from an Access database, and then sort the data in this table into multiple text files. The clearing is that the file name for the record depends on the values in each record. This is officially my first C # application, so you can consider me "green." I should also mention that I work on the Access database until I get the code that eventually exits the SQL server with millions of records.
The code now works for me, but the problem is that there are a ton of file open / close operations. I only want to open each file once for recording, since it will write these files to a network drive. This, in fact, is an application for gluing on a server, so there are other limitations - I can’t save to a local disk and then copy it to the network. I cannot sort the request before pulling. I cannot adversely affect server resources while I work.
Probably the best way to do this is with a hash table. Check if the file was opened, if not, open it and save the file descriptor in the hash table. Then close them all at once when done. However, I cannot find an example of how to use several StreamWriter objects at the same time.
I expected to find an answer to this relatively easily, but I can not find a solution for it. My suspicion is that StreamWriter is the wrong class to be used for this.
The closest previous question I could find is on the CodeProject page . On this page they say that the practice of storing open hands of files is bad and should be avoided, but the page does not explain why they do not offer an alternative to the example. There is a proposal to load the entire data set into memory, and then work with it, but this is not an option for me, since there will be too much data in the tables.
Here is what I still have.
String strConnection;
String strQuery;
String strPunchFileNameTemplate;
strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ClockData.accdb";
strQuery = @"SELECT * FROM ClockPunches";
strPunchFileNameTemplate = @"C:\PUNCHES\%READER%.TXT";
using (OleDbConnection ConnObj = new OleDbConnection(strConnection)) {
OleDbCommand CmdObj = new OleDbCommand(strQuery,ConnObj);
ConnObj.Open();
using (OleDbDataReader ReaderObj = CmdObj.ExecuteReader(CommandBehavior.KeyInfo)) {
DataTable TableObj = ReaderObj.GetSchemaTable();
while(ReaderObj.Read()) {
DateTime dtTime = ReaderObj.GetDateTime(ReaderObj.GetOrdinal("PunchTime"));
Int16 intID = ReaderObj.GetInt16(ReaderObj.GetOrdinal("CardNumber"));
String strReader = ReaderObj.GetString(ReaderObj.GetOrdinal("Device"));
strReader = GetDeviceFileName(strReader);
String pathStr = strPunchFileNameTemplate.Replace("%READER%",strReader);
Boolean FileExistedBool = File.Exists(pathStr);
using (StreamWriter outSR = new StreamWriter(pathStr, true)) {
if (FileExistedBool == false) {
outSR.WriteLine("EXAMPLE FILE HEADER");
}
String outputStr = dtTime.ToString("MM-dd-yyyy HH:mm:ss") + " " + intID.ToString("000000");
outSR.WriteLine(outputStr);
}
}
}
}