Hi EEE:
I have a problem. I am loading a csv file to the database in .NET. By definition a csv file is a comma seperated file which can be created in a simple text editor like notepad or also in excel and saved as a csv. Now, when I read in the file (see code below) I will get the following error because the file is expected as an excel sheet. How can I make it generic to open a csv regardless of its file origin - text or excel.
Code:
// Read into dataset the first sheet of Excel
//
string connectionString;
string commandText;
System.Data.OleDb.OleDbCon
nection conn;
System.Data.OleDb.OleDbCom
mand command;
connectionString = "Provider=Microsoft.Jet.OL
EDB.4.0;Da
ta Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
commandText = "select * from [sheet1$]";
conn = new System.Data.OleDb.OleDbCon
nection (connectionString);
command = new System.Data.OleDb.OleDbCom
mand (commandText, conn);
conn.Open ();
System.Data.OleDb.OleDbDat
aAdapter da = new System.Data.OleDb.OleDbDat
aAdapter (commandText, conn);
DataSet ds = new DataSet ();
da.Fill (ds, "FileData");
Error:
[OleDbException (0x80004005): External table is not in the expected format.]
System.Data.OleDb.OleDbCon
nectionInt
ernal..cto
r(OleDbCon
nectionStr
ing constr, OleDbConnection connection) +1059617
System.Data.OleDb.OleDbCon
nectionFac
tory.Creat
eConnectio
n(DbConnec
tionOption
s options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) +53
System.Data.ProviderBase.D
bConnectio
nFactory.C
reateNonPo
oledConnec
tion(DbCon
nection owningConnection, DbConnectionPoolGroup poolGroup) +27
System.Data.ProviderBase.D
bConnectio
nFactory.G
etConnecti
on(DbConne
ction owningConnection) +47
System.Data.ProviderBase.D
bConnectio
nClosed.Op
enConnecti
on(DbConne
ction outerConnection, DbConnectionFactory connectionFactory) +105
System.Data.OleDb.OleDbCon
nection.Op
en() +37
Start Free Trial