copy CSV file to MS Access Table

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
using C# I am trying to create a console app that reads a CSV file from a specific folder location and import these records into a MS Access Table. Once the records in the file have been imported successfully I will then delete the .csv file. <br/>
<br/>
So far this is what I have:<br/>
<pre class="prettyprint" style=" [/code]
<pre id="x_pre0" lang="c#" style="background-color:#fbedbb; padding:6px; font-size:9pt; line-height:normal; font-family:Courier New,Courier,mono; white-space:pre-wrap; border:1px solid rgb(251,237,187); word-wrap:break-word <span class="x_code-keyword" style="color:blue public <span class="x_code-keyword" style="color:blue static DataTable CsvFileToDatatable(<span class="x_code-keyword" style="color:blue string path, <span class="x_code-keyword" style="color:blue bool IsFirstRowHeader)<span class="x_code-comment" style="color:#008000; font-style:italic //<span class="x_code-comment" style="color:#008000; font-style:italic here Path is root of file and IsFirstRowHeader is header is there or not
{
<span class="x_code-keyword" style="color:blue string sql = <span class="x_code-keyword" style="color:blue string.Empty;
DataTable dataTable = <span class="x_code-keyword" style="color:blue null;
<span class="x_code-keyword" style="color:blue string databaseName = <span class="x_code-keyword" style="color:blue string.Empty;
<span class="x_code-keyword" style="color:blue string pathOnly = <span class="x_code-keyword" style="color:blue string.Empty;
<span class="x_code-keyword" style="color:blue string fileName = <span class="x_code-keyword" style="color:blue string.Empty;

<span class="x_code-keyword" style="color:blue try
{
databaseName = Path.GetDirectoryName(ConfigurationManager.AppSettings[<span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple DatabaseName"]);
pathOnly = Path.GetDirectoryName(ConfigurationManager.AppSettings[<span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple QuantumOutputFilesLocation"]);
fileName = Path.GetFileName(ConfigurationManager.AppSettings[<span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple CSVFilename"]);

sql = <span class="x_code-string" style="color:purple @"<span class="x_code-string" style="color:purple SELECT * FROM [" + fileName + <span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple ]";

<span class="x_code-keyword" style="color:blue using (OleDbConnection connection = <span class="x_code-keyword" style="color:blue new OleDbConnection(<span class="x_code-string" style="color:purple @"<span class="x_code-string" style="color:purple Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + databaseName + <span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple "))
{
<span class="x_code-keyword" style="color:blue using (OleDbCommand command = <span class="x_code-keyword" style="color:blue new OleDbCommand(sql, connection))
{
<span class="x_code-keyword" style="color:blue using (OleDbDataAdapter adapter = <span class="x_code-keyword" style="color:blue new OleDbDataAdapter(command))
{
dataTable = <span class="x_code-keyword" style="color:blue new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
<span class="x_code-comment" style="color:#008000; font-style:italic //<span class="x_code-comment" style="color:#008000; font-style:italic this is what Im trying
adapter.InsertCommand = <span class="x_code-keyword" style="color:blue new OleDbCommand(<span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple INSERT INTO tblQuantum (DateEntered, SerialNumber, ModelNumber, BatchNumber, DeviceType, RatedPower, EnergyStorageCapacity " +
<span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple MaxEnergyStorageCapacity, User_IF_FWRevNo, Charge_Controller_FWRevNo, RF_Module_FWRevNo, SSEGroupNumber, TariffSetting) " +
<span class="x_code-string" style="color:purple "<span class="x_code-string" style="color:purple VALUES (?, ?)"); <span class="x_code-comment" style="color:#008000; font-style:italic //<span class="x_code-comment" style="color:#008000; font-style:italic how do i read each row in the dataTable to retrieve the necessary values for each column??
adapter.Update(dataTable);
}
}
}
}
<span class="x_code-keyword" style="color:blue finally
{

}
<span class="x_code-keyword" style="color:blue return dataTable;
}[/code]
Can I just go ahead an save the datatable to a table I have created in the Access DB? How would I go about doing this? Any help would be great<br/>

View the full article
 
Back
Top