It is using c# programming. I am selecting the csv file and inserting it in to the access db. I have say, 6 columns in the csv file like ID, FName, LName, Address, Zipcode, cell number in the same order. In access, my columns are FirstName, LastName, S_NO, zip_code (in the same order). How i can import this csv file in to access db, if the column names are different as well the order is different. How can i implement this?? Pls suggest. I tried the following code:
enter code here
DataSet da = new DataSet(); try {
da = this.ConnectCSV(strCSVFile);
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\EmpApp\\EmpData.accdb;";
OleDbConnection conn = new OleDbConnection(connstring);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
conn.Open();
for (int i = 0; i <= da.Tables["Ss"].Rows.Count - 1; i++)
{
for (int j = 1; j <= da.Tables["Ss"].Columns.Count - 1; j++)
{
cmd.CommandText = "Insert into EMP_DOWNLOAD ( ID,Company_name,month_billed,year,start_date,end_date,Designation"
+ ") values(" + (i + 1) + ",'"
+ da.Tables["Ss"].Rows[i].ItemArray.GetValue(0) + "',"
+ da.Tables["Ss"].Rows[i].ItemArray.GetValue(8) +
da.Tables["Ss"].Rows[i].ItemArray.GetValue(9) + ")";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
btnUpload.Enabled = false;
}
enter code here
I would use a Linq statement to break your text apart and create arrays of your parameter objects then loop through the arrays adding the parameters to each insert statement that is sent to the database.
Like this
With MS Access, you can run straight from CSV to a table:
You can pass null for the filed which are not required..