When I am trying to open a excel into one of my windows service using following code it is throwing "Value cannot be null. Parameter name: source" on objConn.Open(); Can any one please help me.
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
LogManager LogWrite = new LogManager();
try
{
string conn = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Repository\RuleExcel\Rules_Repository_2018-06-28_03-41-29-133.xlsx;Extended Properties='Excel 12.0;HDR=YES;';";
LogWrite.WriteLog(conn);
// Create connection object by using the preceding connection string.
objConn = new OleDbConnection(conn);
LogWrite.WriteLog(objConn.DataSource);
// Open connection with the database.
objConn.Open();
try this below code, it works for me :
using (OleDbConnection objConn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + #";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""))
{
objConn.Open();
}
Related
The current problem I'm having is an error message that it could not find file C: now I dont know what the problem is because the file is in that location. I have I tried in both .accbd and .mbd.
private static OleDbConnection GetConnection()
{
OleDbConnection conn = new OleDbConnection();
String connectionString =
#"Provider=Microsoft.JET.OlEDB.4.0;"
+ #"Data Source= C:\Temp\F1\Docs\Expeditors Project\Table1.accbd";
conn = new OleDbConnection(connectionString);
conn.Open();
return conn;
}
Do you tried another Provider?
For example:
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Temp\F1\Docs\Expeditors Project\Table1.accbd;
try \\ in Data Source Path
like below -
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Data\test.mdb;Persist Security Info=False");
try this `
`private static OleDbConnection GetConnection() throws SQLException{
{
if (conn==null)
{
try{ OleDbConnection conn = new OleDbConnection();
String connectionString = #"Provider=Microsoft.JET.OlEDB.4.0;"
+ #"Data Source= C:\Temp\F1\Docs\Expeditors Project\Table1.accbd";
conn = new OleDbConnection(connectionString);
conn.Open();
return conn;
}}
catch(Exception e){
e.printStackTrace();
}
string connStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
OleDbConnection MyConnection;
OleDbCommand MyCommand = new OleDbCommand();
MyConnection = new OleDbConnection(#connStr);
MyConnection.Open(); // Here the error come...
Im try to using this connStr to connect to My xls file. The problem was "External table is not in the expected format."
Im have aleady installed AccessDatabaseEngine_x64. But the error still remain.
How to connect to this .xls file ?
I have an excel file and an oledb connection to it. When reading data while file is opened in windows, it throws the following error (at Adapter.Fill method).
However, the code runs fine when file is not opened manually.
private System.Data.DataSet GetExcelData()
{
// Create new DataSet to hold information from the worksheet.
System.Data.DataSet objDataset1 = new System.Data.DataSet();
DataTable dt = new DataTable();
try
{
string path = ConfigurationManager.AppSettings["ExcelFilePath"];
//string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
OleDbConnection objConn = new OleDbConnection(connectionString);
objConn.Open();
//String strConString = "SELECT * FROM [Data Version5.2$A2:ZZ] where [Status] = 'aa'";//Status
String strConString = "SELECT * FROM [Data Version5.2$A2:ZZ] where [Status] IS NULL OR [Status]='SubReport'";//Status SubReport
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Fill the DataSet with the information from the work sheet.
objAdapter1.Fill(objDataset1, "ExcelData");
objConn.Close();
}
catch (Exception ex)
{
throw ex;
}
return objDataset1;
}
The error message is
Assuming you don't need to write to the file, try adjusting your connection string to include the read only mode (Mode=Read). I have that in all of mine (where I don't need to write to the file) and I've never had a problem reading from workbooks that are already open:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
path + ";Mode=Read;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
I also don't tend to read Excel files as XML, so the Extended Properties for my connection strings are Excel 12.0;HDR=YES;IMEX=1;
How can I replace D:\temp\test.xls with filePath in OleDbConnection statement.
I can get the exactly filePath (with OpenFileDialog, then I can located my .xls file conveniently, no more hardcoded), but when I insert the variable filePath as Style2, it doesn't work. How can I fix this ? Thanks.
Style1
OleDbConnection dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties=""Excel 8.0;HDR=Yes;""");
Style2
OleDbConnection dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;""");
[Updated]
Some part of my code as this,
DataTable fooData = new DataTable();
OleDbConnection dbConnection = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filePath;Extended Properties=""Excel 8.0;HDR=Yes;""");
dbConnection.Open ();
try
{
OleDbCommand dbCommand = new OleDbCommand("SELECT * FROM [maleSheet$]", dbConnection);
OleDbDataReader dbReader = dbCommand.ExecuteReader();
int RankIndex = dbReader.GetOrdinal("Rank");
while (dbReader.Read())
{
string rank = dbReader.GetValue(RankIndex).ToString();
////....
}
}
Error as below at line OleDbDataReader dbReader = dbCommand.ExecuteReader();
An unhandled exception of type
'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
OleDbConnection dbConnection = new OleDbConnection( String.Format( #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;""", filePath ) );
i have the following problem with an excel file that i want to uppercase the values of its respective cells in C# with the next code:
DbProviderFactory factory =
DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connection = factory.CreateConnection();
string stringConnection = String.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=NO;IMEX=1'",
excelPath);
connection.ConnectionString = stringConnection;
connection.Open();
DbCommand updateExcel =
factory.CreateCommand();
updateExcel.CommandText =
"UPDATE [sheet1$] SET lastname = UCASE(lastname), name = ucase(name)";
updateExcel.Connection = connection;
updateExcel.ExecuteNonQuery();
conection.Close();
connection.Dispose();
and it raise an oledbexception about parameters doesnt specified, anybody could help me?
If you are trying to connect to an Excel 2007 or 2010 spreadsheet you will need the following connection string...
OleDbConnection xlconnection = new OleDbConnection();
xlconnection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + #";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"