How to iterate through a folder with .db files and convert them to .csv?

  • Thread starter Thread starter samiarja
  • Start date Start date
S

samiarja

Guest
I have around 1,186 files with .db file format. These files need to be converted to .csv in order to do further data processing.

Until now one file was successfully converted to CSV, using SQLiteConnection and SQLiteDataAdapter, all the credit goes to Jack J Jun for helping me with it.

In the button click event, the .db file path is set and the SQLiteConnection was used to connect to the SQLite server, as shown below

string _filePath = @"C:\temp\Data2\DADLoggerDB_2000-01-01T00_00_15.db";
SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=" + _filePath);
m_dbConnection.Open();
SQLiteDataAdapter adapter = new SQLiteDataAdapter("SELECT * FROM DADLoggerTable;", m_dbConnection);
DataSet set = new DataSet();
adapter.Fill(set);
DataTable table = set.Tables[0];
//Where the csv file will be saved
string filepath = @"C:\temp\Data2\CSV\";
SaveCsv(table, filepath);
MessageBox.Show("Converted");

and a SaveCsv method is implemented as below:

public void SaveCsv(DataTable dt, string filePath)
{
FileStream fs = null;
StreamWriter sw = null;
try
{
fs = new FileStream(filePath + dt.TableName + ".csv", FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, Encoding.Default);
var data = string.Empty;
for (var i = 0; i < dt.Columns.Count; i++)
{
data += dt.Columns.ColumnName;
if (i < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
for (var i = 0; i < dt.Rows.Count; i++)
{
data = string.Empty;
for (var j = 0; j < dt.Columns.Count; j++)
{
data += dt.Rows[j].ToString();
if (j < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
}
}
catch (IOException ex)
{
throw new IOException(ex.Message, ex);
}
finally
{
if (sw != null) sw.Close();
if (fs != null) fs.Close();
}
}

Until now I can convert only one file to .CSV at a time, Ultimately all the .db files need to be converted to .csv automatically.

I appreciate any help in this regards.

Continue reading...
 
Back
Top