I have a problem, when I want get some data from CSV file to Database, my setp as fllow:
- I get this CSV file from special path
- insert to a datatable
- transfer data from datatable to database
now. when I access to no.2, i find 2 coulmns value is blank, who can help me??
Thanks
my code:
DataTable dt = new DataTable();
string _filePath, _fileName;
_filePath = strFileName.Substring(0, strFileName.LastIndexOf(@"\") + 1);
_fileName = strFileName.Substring(strFileName.LastIndexOf(@"\") + 1);
string excelConnectionString;
//string strFileName = @"D:\TestFile\Prelim\USG\aaa.xlsx";
//string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + _filePath + @"\" + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
try
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + _filePath + @"\" + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
}
catch
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + _filePath + @"\" + ";Extended Properties=\"Text;HDR=Yes;FMT=Delimited\"";
}
try
{
OleDbConnection excelConnection = null;
using (excelConnection = new OleDbConnection(excelConnectionString))
{
OleDbDataAdapter apter = new OleDbDataAdapter("Select * from " + _fileName, excelConnection);
apter.Fill(dt);
}
}
catch (Exception ex)
{
throw;
}
return dt;
first step is to remove the noise. both try/catches are useless. the first one will never throw because you are simply concatenating strings. the second will provides no value it’s the same as not catching at all.
i believe you want to use the name of the worksheet for the tablename, not the filename. and once the datatable is filled you then need to save those records to the database.
then you can use this table to populate the database