Getting different errors while importing Excel file through SqlBulkCopy - c#

I am importing an Excel file into SQL Server using SqlBulkCopy
But every time I get differant errors like
Cannot update. Database or object is read-only.
or
Could not find installable ISAM.
or
The Microsoft Jet database engine could not find the object. Make sure the object exists and that you spell its name and the path name correctly.
Check my code below...
String strConnection = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
string excelConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filepath+";Extended Properties=Excel 8.0;HDR=YES;";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand
("Select * from [Sheet1$]",
excelConnection);
MessageBox.Show("ss");
excelConnection.Open();
MessageBox.Show("ss2");
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
MessageBox.Show("ss1");
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
sqlBulk.DestinationTableName = "RecExcelTable";
//sqlBulk.ColumnMappings.Add("ID", "ID");
//sqlBulk.ColumnMappings.Add("Name", "Name");
sqlBulk.WriteToServer(dReader);
Also I need to compitable this code for any version of Excel.
How can I do that and solve my error?

I had similar issue. Check that the file path is correct
do like
C:\\file.xls
as a test.
And also check that the version is correct. 8.0 is pretty old I think thats like office 2000, no ?

Related

Read a dbase IV file C#

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;";

C# ace oledb 12 read-only file

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.

alternate for Oleb DB importing excel file

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

Get data from the excel file using c# asp.net

I am trying to load data using following code.
string path = System.IO.Path.GetFullPath(uploadExcelFile.PostedFile.FileName);
string connString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Rizwan shahid\\Desktop\\DataUpload\\Req.xls;Extended Properties=Excel 12.0;";
OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "Table");
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
oledbConn.Close();
}
It was working on 32Bit operating system but when run this code on 64Bit OS it gives the following error
The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.
I am running VS in Administrator mode and found many solution like replace Sheet1 with file name or place file in C drive but still getting the same error.
you can download latest version here of Jet
http://www.microsoft.com/en-us/download/search.aspx?q=jet
This works (I start out with a 'dummy' path and then apply the real runtime path):
OleStringBuilder =
new OleDbConnectionStringBuilder(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
OleStringBuilder.DataSource = MapPath(#"~\App_Data\MyExcelWorksheet.xls");

Excel contents into SQL Server table using Visual Studio 2008

I'm creating an application where the user clicks on a button, browses for an Excel file, and the data is copied into the data table created in the database.
I am using VS2008 and SQL Server 2005.
I wrote code for opening the file of course, and created a dataTable and its dataColumns in the .cs file. What else should I do?
Thank you.
You could do as Krishna advises... but that code will only work if the columns in the excel file and Database columns are the same in number and order. For ease of maintainability and mapping down the line I highly recommend you use a combination of Linq to Excel and Linq to SQL as in this article:
http://solidcoding.blogspot.com/2008/01/linq-to-excel-sql-import.html
You can write like the code below to dump the data into the SQL Server table
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("ImportFile.xls") + ";Extended Properties=""Excel 8.0;HDR=Yes;""";
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand("Select * FROM [NameOfTheDataSheet$]", connection);
connection.Open();
using (DbDataReader dataReader = command.ExecuteReader())
{
string sqlConnectionString = "SQL Connection String";
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelDataTable";
bulkCopy.WriteToServer(dataReader);
}
}
}
Let me know if you have different requirements.

Categories

Resources