Convert string to nvarchar (import excel to mssql) - c#

I have been trying to find a way to convert data type string to nvarchar while I am importing data from an excel spreadsheet. So far I have this piece of code to import the data through C#.
// Connection String to Excel Workbook,Replace DataSource value to point to your excel file location
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Folder\\Folder\\Excel_File.xls ;Extended Properties=Excel 8.0";
// Create Connection to Excel Workbook
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("Select * FROM [Sheet1$]", connection);
connection.Open();
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=True";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "Table_Name";
bulkCopy.WriteToServer(dr);
MessageBox.Show("Data Exported To Sql Server Successfully");
}
}
I have looked for ways to convert from string to nvarchar but the only examples that I have found are only for converting string collected from textboxes or through consoles. I do have one column though in date type but I have compensated for it in MSSQL.
If anyone could give me a sample code of what I should be doing it would be greatly appreciated.

My suspicion is that you are 'tripping' over some hidden characters that Excel is inserting in your text strings. Try logging the imported data to ensure that it's clean.

Related

Is it possible to use OLEDB to read text from an excel file that contains images?

I can currently read excel files that contain only text using the OLEDB approach. However, I now need to read files that contain images. I still only require the text/numeric values from the file. I receive an error when opening the OleDbConnection.
So the connection string I am using is the following:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=filename.xlsx;Extended Properties="Excel 12.0;HDR=NO;IMEX=1;TypeGuessRows=0;
System.Data.OleDb.OleDbException: 'External table is not in the expected format.'
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(GetSQLCommandString(conn, sheetName), conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd))
{
adapter.Fill(dt);
}
}
}
return dt.AsEnumerable()
.Select(r => new Row(r));
So it fails with that error, but once I remove the images it works as expected.
Should I be looking to solutions such as OPEN XML, or is there a way I can alter my OleDb approach to work?

C# Bulk Copy via OleDBDataReader from large excel file throwing out of memory exception

I have a large excel file(530K Rows with a lot of columns). Ends up being 247MB in .xlsb format. I am attempting to import to SQL Server using BulkCopy in C#, however I am having issues where the datareader ends up running out of memory before it even starts reading the file once I run the ExecuteReader() command.
string exlConnString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={_filepath};Extended Properties=\"Excel 12.0;HDR=YES;\"";
string sqlQuery = $"SELECT * FROM [{SheetName}]";
using OleDbConnection conn = new OleDbConnection(_connstring)) {
OleDbCommand exlCmd = new OleDbCommand(sqlQuery, conn)
conn.Open();
OleDbDataReader dr = exlcmd.ExecuteReader(); <---NEVER GETS PAST THIS LINE BEFORE RUNNING OUT OF MEMORY.
SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnString);
bulkCopy.DestinationTable = TableName;
while(dr.Read()) {
bulkcopy.WriteToServer(dr);
}
dr.Close();
}
I am running in x86 mode because I was geting an error that the ACE Database was not installed on my local machine and corporate policy restrictions prevent me from downloading and installing the needed file to run it in x64 mode.
The code works perfectly fine when I test it on smaller files, but not when I test it on this bigger file, so it definitely is the filesize causing the issue. Any suggestions or help would be appreciated. Doesn't make much sense that a bulk copy runs out of memory when it is meant for handling large sets of data, which also means that the filesize is going to be large as well...
And yes, I know I SHOULD be able to import this using OPENROWSET or OPENDATASOURCE in SQL Server but THAT is ALSO Turned off and they will not enable it, so this is not an option.
So your problem is next.
When you try to ExecuteReader DataReader attempt to read all data from your excel file to memory. You could think about this, like a speciality working with excel through a OleDbProvider.
So my suggestion is to work with csv files instead of excel, because with csv file you have an ability to read and parse them line by line. For this aim i would recommend you to use CSV helper
Refer this code..
Here dtExcelData is datatable variable & da is OleDbDataAdapter variable.
string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={_filepath};Extended Properties='Excel 12.0;HDR=YES';";
// Create Connection to Excel Workbook
using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
{
connection.Open();
da = new OleDbDataAdapter("Select * FROM [Sheet1$]", connection);
da.Fill(dtExcelData);
//store data in sql server database table
// below connection string "conString" is I mention in app.config file.(sql server connection string to store data in sql server database)
string str = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(str))
{
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(con))
{
bulkCopy.DestinationTableName = "TableName";
con.Open();
bulkCopy.WriteToServer(dtExcelData);
con.Close();
}
}
connection.Close();
}
Mark it as a answer if it is useful to you. :)

Excel sheet to SQL table

I get the Excel sheet with data uploaded from UI, I need to access that sheet through WCF service and insert the data contained in it into a SQL table.
Kindly guide how this can be done.
I am facing challenge in reading that Excel sheet through WCF service.
I have used below code to read a Excel sheet from a local drive. Change Source property to your FTP location of your excel sheet.
string con = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\MyData.xlsx;" +
#"Extended Properties='Excel 8.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
// Do your things here
}
}
}
Note: .xls has data limitations whereas .xlsx supports more data rows in a sheet. You need to install Microsoft Office Access database engine 2007 if you are going to read .xlsx files
string UploadedFilePath = FullPathOfExcelOnTheServer;
string ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + UploadedFilePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1;FMT=Delimited\"";
using (OleDbConnection oledbConnExcel = new OleDbConnection(ExcelConn))
{
oledbConnExcel.Open();
using (OleDbDataAdapter oledbAdapterExcel = new OleDbDataAdapter("SELECT * from [" + SheetName + "$]", oledbConnExcel))
{
using (DataTable dtblSheetData = new DataTable())
{
try
{
oledbAdapterExcel.Fill(dtblSheetData);
}
catch (Exception lexQuery)
{
}
}
}
oledbConnExcel.Close();
}
Fact is the is no way to read the excel data through WCF Service. But You can cosider this approaches
You may:
Read all excel data on client side and call WCF service and include all data from excel in parameter as (array of strings)?
You can create macro(VB) in Excel sheet and what call WCF Service and send data through parameter. When You have all logic in one place.
If client side is .NET side You can use atribbute [KnowsTypeAtribbute].Then You can call WCF service with param type as Excel type (Sheet or other). May by use some type form OpenXml dll may by usefull (But I don't know is sheet or other types is serializable. A serializable is require)
You can send to WFC service path to file and read excel file from that path through WCF service.

Format of the initialization string does not conform to the OLE DB specification

I'm getting the following error when trying to read an Excel file using a third-party component
Format of the initialization string does not conform to the OLE DB specification
Now, I know the words "third party component" are going to set off alarm bells here, but hear me out.
Here's the connection string I'm using
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\users\rory\downloads\testdb_4.xls;
Extended Properties='Excel 8.0;HDR=YES;';
I've got this exact connection string to work no problem with the following C# code
using (OleDbConnection conn = new OleDbConnection())
{
DataTable dt = new DataTable();
conn.ConnectionString = connstring;
using (OleDbCommand comm = new OleDbCommand())
{
comm.CommandText = "select * from [TEST_DB$]";
// TEST_DB is the name of the sheet
comm.Connection = conn;
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = comm;
da.Fill(dt);
// do stuff with dt
}
}
}
This works as expected, and the datatable is filled with data from the Excel file. However when I try to access it from the component, I get the error above.
I'm not an expert on OLE DB, but I was under the impression that the drivers are at the OS level and if they work for one app / connection string they should work for all apps with that same connection string. Am I wrong? And if so, does anyone have a clue what's going on here?
I have contacted support for the component.
The solution to this ended up being to replace the single quotes with escaped doubles.
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\users\rory\downloads\testdb_4.xls;" +
"Extended Properties='Excel 8.0;HDR=YES;'"; // single quotes
became
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\users\rory\downloads\testdb_4.xls;" +
"Extended Properties=\"Excel 8.0;HDR=YES;\""; // escaped double quotes
OleDbConnection had no problem with single quotes but apparently some apps and components do.

Updating data in to a Excel sheet using c#

I am trying to update some data in an Excel sheet of the format "xlsx" using OLEDB connection, but I am unable to make out the connection establishment.
Here is my code:
String sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" + "D:\abc1.xlsx" + "';Extended Properties='Excel 8.0;HDR=Yes'";
OleDbConnection con = new OleDbConnection(sConnectionString);
con.Open();
OleDbCommand com = new OleDbCommand("select * from Sheet1",con);
OleDbDataReader reader = null;
reader = com.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0]);
}
con.Open();
Console.ReadLine();
}
When I run the code, I'm facing the following exception:
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local
machine.
Any idea how to recover from this exception or any other suggestions from which i can update my data in excel is advisable.
This could be the provider you have stated try changing it to the one which matches the Excel version on your machine
Try
Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\abc1.xlsx';Extended Properties="Excel 12.0 Xml;HDR=YES";
Instead
Could also be that excel isnt installed
Also check that you have referenced the OLEDB library for your project
Change your PlatformTarget Type from AnyCPU to X86.
Steps:
Goto Project Properties.
Select Build tab.
Select X86 from PlatformTarget options.
It is possible that there are multiple reasons for this exception.
1) You could use the OleDbEnumerator class to find out what providers are available.
Accordingly you set your connection string.
2) Before that just try out below connection string.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + "D:\abc1.xlsx" + "';Extended Properties='Excel 8.0;HDR=Yes'";
3) If you have a 64 bit OS, there is no 64-bit version of the JET provider available and there's no alternative. As long as you want to support JET, you'll need to set the build target to x86.
first save as your excel workbook as Excel 97-2003 Workbook
It will work in my project...
string filepath = Server.MapPath("~/ImportData/") + fileUpload.FileName;
OleDbConnection oconn = new OleDbConnection
(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";
Extended Properties=Excel 8.0");`
oconn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from Sheet1", oconn);
DataSet ds = new DataSet();
adp.Fill(ds);
oconn.Close();`

Categories