Create copy to file method

This commit is contained in:
Lunastra 2021-12-11 22:34:47 -08:00
parent feb2c0abe5
commit 9db6ab7db7
3 changed files with 67 additions and 6 deletions

View File

@ -17,7 +17,7 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
public bool CreateArchiveFolder(){ public bool CreateArchiveFolder(){
string curPath = Directory.GetCurrentDirectory(); string curPath = Directory.GetCurrentDirectory();
//string temp = @ //string temp = @
string archiveFolder = curPath + "\\HobbyArhive"; string archiveFolder = curPath + "\\HobbyArchive";
//Console.WriteLine("Creating a new folder at: {0}", archiveFolder); //Console.WriteLine("Creating a new folder at: {0}", archiveFolder);
if (!Directory.Exists(archiveFolder)) if (!Directory.Exists(archiveFolder))
{ {
@ -30,8 +30,14 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
// Return the name of the new output file with Path // Return the name of the new output file with Path
public string CreateOutFileName() { public string CreateOutFileName() {
try { try {
string path = Directory.GetCurrentDirectory(); string path = Directory.GetCurrentDirectory() + "\\HobbyArchive";
Console.WriteLine("The current directory is {0}", path); Console.WriteLine("The current directory is {0}", path);
string date = DateTime.Now.ToString("M_d_yyyy");
string fileName = date + "_archive.csv";
string filePath = System.IO.Path.Combine(path, fileName);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Filepath: {0}", filePath);
return path; return path;
} }
catch catch
@ -43,6 +49,12 @@ namespace TeamHobby.HobbyProjectGenerator.Archive
// put everything toget here // put everything toget here
public bool Controller(){ public bool Controller(){
// Create the folder for Archiving
CreateArchiveFolder();
// Create the file name for output
CreateOutFileName();
return true; return true;
} }

View File

@ -36,7 +36,7 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
return _conn; return _conn;
} }
// Closing a connection here will cause a problem // TODO: Closing a connection here will cause a problem
// Make sure whoever called this need to close the connection until we can fixed it. // Make sure whoever called this need to close the connection until we can fixed it.
public Object? ReadData(string cmd) public Object? ReadData(string cmd)
{ {
@ -175,7 +175,45 @@ namespace TeamHobby.HobbyProjectGenerator.DataAccess
// Copy the Sql data to the a text file // Copy the Sql data to the a text file
public bool CopyToFile(string filePath){ public bool CopyToFile(string filePath){
return true; try
{
_conn.Open();
// Conver backward slash in to forward slash
filePath = filePath.Replace("\\", "/");
string sqlQuery = "SELECT 'LtimeStamp', 'logID', 'LvName', 'catName', 'userOP', 'logMessage' " +
"UNION ALL " +
"SELECT* FROM log " +
"WHERE DATEDIFF(CURRENT_TIMESTAMP, log.LtimeStamp) > 30 " +
"INTO OUTFILE '" + filePath + "'" +
"FIELDS ENCLOSED BY ''" +
"TERMINATED BY ',' " +
"LINES TERMINATED BY '\r\n';";
Console.WriteLine(sqlQuery);
OdbcCommand command = new OdbcCommand(sqlQuery, _conn);
//command.Parameters.AddWithValue("@filePath", filePath);
//command.Prepare();
Console.WriteLine("Output to a file started. ");
command.ExecuteNonQuery();
Console.WriteLine("Output to a file completed. ");
_conn.Close();
return true;
}
catch
{
_conn.Close();
Console.WriteLine("Error when copying query to a file !!");
throw;
}
finally
{
_conn.Close();
}
} }
public bool RemoveOutputFile(string filePath){ public bool RemoveOutputFile(string filePath){

View File

@ -70,12 +70,23 @@ namespace TeamHobby.HobbyProjectGenerator.Main
// Testing archive Manager // Testing archive Manager
ArchiveController archive = new ArchiveController(datasource); ArchiveController archive = new ArchiveController(datasource);
string curPath = archive.CreateOutFileName();
//Creating the folder Archive //Creating the folder Archive
Console.WriteLine("Creating a new folder ..."); Console.WriteLine("Creating a new folder ...");
archive.CreateArchiveFolder(); archive.CreateArchiveFolder();
Console.WriteLine("");
// Creating a file name:
Console.WriteLine("Creating file name ... ");
string curPath = archive.CreateOutFileName();
string pathForward = @"C:\Users\Chunchunmaru\Documents\csulbFall2021\HobbyProject\Source Code\main\bin\Debug\net6.0\HobbyArchive";
string pathTemp = "C:/Temp/oldlogs10.txt";
string pathTempBack = @"C:\Temp\oldlogs10.txt";
Console.WriteLine("----------------");
//Output SQL to a text file
sqlDS.CopyToFile(curPath);
// 2.Inserting Data into the database: // 2.Inserting Data into the database: