I have a SSIS package with script task. c# script use ACE Oledb 12.0 provider to connect to excel file. The question is, how to connect to excel file in read-only mode (if someone open the file, my script should not have an error - it should work). The code, I tried here:
string fileToTest = Dts.Variables["User::FileName"].Value.ToString();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + fileToTest + #";Extended Properties=""Excel 8.0;READONLY=1""";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open();
string sqlQuery = "SELECT * FROM [SheetName$A1:FZ1000]";
OleDbDataAdapter dataAdt = new OleDbDataAdapter(sqlQuery, excelConnection);
DataSet dataSt = new DataSet();
dataAdt.Fill(dataSt, "TblName1");
DataTable dataTbl = dataSt.Tables["TblName1"];
I receive oledbexception, if someone open the file.
Use google to search for that.
I searched for: "microsoft.ace.oledb.12.0 read only"
https://social.msdn.microsoft.com/Forums/office/en-US/498cd52a-b0ee-4c8d-8943-2b76055b4130/oledbconnection-read-only-mode?forum=accessdev
It looks like you can add to the connection string.
From that page:
Actually, with an OleDbConnection (assuming .net here). You can specify a read only mode in your connection string of the OleDbConnection. The following connection string will prevent you from changing data in your datasource:
const string cnnString = "Provider=Microsoft.ACE.OLEDB.12.0"
+ ";Mode=Read"
+ #";Data Source=|DataDirectory|\Northwind 2010.accdb";
It looks like adding ;Mode=Read to the connection string should do the trick.
Related
I am developing an application to recover data from a DBF file.
I did research on the Internet that sent me to this link : enter link description here
I applied this code but nothing helps it doesn't work :/
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test\users.dbf;Extended Properties=dBASE IV;User ID=;Password=MyPassword;";
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from users.dbf";
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
DataSet ds = new DataSet(); ;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
I pass this code in a Try and Catch and it returns me this error: Unable to start your application. The workgroup information file is missing or opened exclusively by another user.
the error is caused when trying to open the connection. However the file is neither opened nor used by anyone else.
thank you in advance ;)
Try to remove the file name in the connection string. According to the documentation, The "Data Source" property should only contain the path.
string constr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Test;Extended Properties=dBASE IV;User ID=;Password=MyPassword;";
Maybe someone can help me with the following problem:
I want to import a excel file and read the columns in to the database. I used OleDB provider so far:
string constr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
using (OleDbConnection conn = new OleDbConnection(constr))
{
conn.Open();
OleDbCommand command = new OleDbCommand("Select * from [Sheet1$]", conn);
OleDbDataReader reader = command.ExecuteReader();
is there an alternate for doing this without OleDB? im getting the following errormessage:The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. and i dont have the option to install ole DB on the environment where the application is running.
thanks
I am trying to replicate a defunct homegrown program that I don't have access to the source code. Basically I need to read in a SQL file (here denoted as querySqlAddresses[i]), execute it and dump the result into a specific sheet in a file that I have open.
I'm finding a lot of dead end things, but I think there may be promise in this, I am just not sure HOW to drop the "results" or even what the "results" variable is so I can target it. Does this even make sense?
string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo(querySqlAddresses[i]);
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
I actually found another way of doing it using OleDB connections and passing the User Name, Password and Table space into the connection string.
string connectionString = "Provider=OraOLEDB.Oracle;Data Source=" + tableSpace + ";User Id=" + userName + ";Password=" + password + ";";
System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(connectionString);
cnn.Open();
System.Data.OleDb.OleDbDataAdapter Dadpt = new System.Data.OleDb.OleDbDataAdapter(readText, cnn);
DataSet ds = new DataSet();
Dadpt.Fill(ds);
cnn.Close();
Have no idea what I'm doing wrong here but I keep getting the following exception on the connection.Open(); line:
IErrorInfo.GetDescription failed with E_FAIL(0x80004005)
Problem is I have almost the exact same code in another batch job and it works fine. We even pull a spreadsheet from the same location. Does anyone see anything wrong with my connection or query string?
static void Main(string[] args)
{
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= \\prdhilfs03\l&i-sales&mkt\WORKAREA\Agencyservices\Shared\AIC\Analysts_and_Reporting\Realignments\2014\MassUpdateTesting\ZipCodeTest.xslx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
string queryString = "SELECT * FROM [Query1$]";
try
{
OleDbDataReader reader;
using (OleDbConnection connection = new OleDbConnection(connString))
{
//Set connection objects to pull from spreadsheet
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
For good measure here's a screen shot of the workbook I'm trying to connect to
Did you try to check if you can access the file?
string xlFile = #"\\prdhilfs03\l&i-sales&mkt\......\ZipCodeTest.xlsx";
Console.WriteLine(File.Exists(xlFile) ? "Excel File exists." : "Excel File does not exist.");
I am trying to fetch information Excel 2007 document, suppose my code is as given below:
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + path+ " ; Extended Properties=Excel 12.0;HDR=YES");
con.Open();
OleDbCommand cmd = new OleDbCommand("Select Name from Table$ where Number = "+textBox1.Text +"",con);
label1.Text = cmd.ExecuteScalar().ToString();
con.Close();
Now on con.open(); I am getting Exception as Could not find installable ISAM.
what does this mean and what should I do to resolve this issue.
Try putting single quotes around the datasource and double quotes around the Extended Properties:
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" + path+ "'; Extended Properties=\"Excel 12.0;HDR=YES\"");
also check the installed JET version:
http://support.microsoft.com/kb/239114/en-us
http://support.microsoft.com/kb/209805/en-us