c#, oledb connection string issue - c#

I'm using .net4.0 and c# language.
In my code i have a connection string
oleConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;
Data Source = " + filepath + ";
Extended Propertie s= \"Excel 12.0;HDR=yes\"";
and it work well. But when i change a connection string like this:
oleConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;
Data Source =" + filepath + ";
Extended Properties =\"Excel 12.0;HDR=no\"";
(I change HDR parameter for "no")
I got error: No value given for one or more required parameters.
error from "Microsoft Office Access Database Engine".

If your referencing a column using say [A1] then this will fail. With HDR=No the columns are referenced as F1, F2 etc.

OleDb Connection HDR Default is YES ans there is no option for that.
Check:
Connection strings for Access 2007
Regards

There is no HDR=no....
The default behavioour is no headers. So just leave out the HDR part completely - that will also mean no headers.
More info:
http://msdn.microsoft.com/en-us/library/ms254500.aspx

Related

oldbreader get the text cells as empty

I am trying to read an excel file using oledbreader and oledbconnection the connection string is as follows
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0;"
when I try reading from the excel file some of the files work fine and give me the data I need but in other cases all columns that have a text value are shown as empty but it takes all the int or double values normally and without problems I looked around and found that some people changed the connection string to this
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + PrmPathExcelFile + #";Extended Properties=""Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text"""
and after that it worked just fine. I tried that way but when I try to open connection I get isam error.
The file I read from is xlsx not xls if that helps ..
use this as connection string,
Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+FilePath+";Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1"
you are trying to import mixed types but while type guessing rows the column is datatype is treated as double, so text values are not coming.Using IMEX=1 solves this problem.

Unexpected error from external database driver (22)

Here's my code which is in the beginning of a method for converting .xls file to .csv.
sourceFile="C:\\Users\\myUser\\Desktop\\Folder\\myFile.xls";
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
And it crashes on the last line, throwing this exception: Unexpected error from external database driver (22).
I tried removing the IMEX=1 part, but it still didn't work.
What is the problem?
I also had the same issue but I had to rename the spreadsheet to a shorter name, then it worked.(SQL 2012 Dev)
Strangely enough, I replaced the file in another folder and it worked. I have no idea why this is happening.
Try to use Microsoft.Jet.OLEDB.4.0 provider. Also I would advise you to use OleDbConnectionStringBuilder to build OleDbConnectionString:
var oleConnectionStringBuilder = new OleDbConnectionStringBuilder { Provider = "Microsoft.Jet.OLEDB.4.0" };
oleConnectionStringBuilder.DataSource = sourceFile;
oleConnectionStringBuilder.Add("Extended Properties", "Excel 8.0");
oleConnectionStringBuilder.Add("HDR", "No");
It seems that there is something wrong with the Microsoft Excel Driver. Try to execute the program in another PC to see whether this error happens.
Please take a look at this KB article:
http://www.codeproject.com/KB/database/ReadExcel07.aspx . You can use OleDb to connect the Excel file.
I hope this can help you and feel free to follow up after you have tried.
i have fixed this by uninstalling Microsoft access DB Engine Security update.Also Uninstall Service pack3 Update of access DB engine 2007. Hope this will work...
In my case I renamed excel file's sheet name(it was too long), after that it worked.
I'm using :
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0 Xml; IMEX=1;Importmixedtypes=text;\"";

OledDbDataAdapter "Syntax error in FROM clause" exception

I'm using OleDB to import data into grid from text file with extension ".K$$".
Here's some example code:
FileInfo file = new FileInfo(filename);
string connectionString = "";
OleDbDataAdapter adapter;
OleDbConnection con;
connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + file.DirectoryName + ";Extended Properties=\"Text;Format=TabDelimited;\"";
con = new OleDbConnection(connectionString);
con.Open();
adapter = new OleDbDataAdapter(String.Format("SELECT * FROM {0} ", file.Name), con);
adapter.Fill(MyDataTable);
when executing the Fill method it throws the exception. What's wrong with the FROM clause? Thanks
EDIT:
Ok, after some tests I found out that the problem is with the "$" symbols. Maybe it's some reserved symbol ?
Also, if I rename the extension to ".txt" the file got loaded into the grid but it only have 1 column , which means it can't see that there're tabs in the rows.
Another issue is that when I change the file extension to something different than ".txt" (for ex. ".tx") the Fill method throws exception "Cannot update. Database or object is read-only".
OK, I just tried creating an example.K$$, and then tried to connect to it using the same provider as stated through Server Explorer in Visual Studio 2010. Its an Unrecognised format.
I don't think this will ever work.
You may need to look at connecting via a different provider or method.
I think You should look at this link :-
EDIT :
http://www.codeproject.com/Articles/6737/Fill-a-DataSet-from-delimited-text-files
It will allow you to read your txt file into a datable correctly.
Try checking the path of the filename if its correct
Check that neither the directory, nor the filename, contain spaces. If they do, you'll need to escape/quote them.
You'll also need to quote the filename if it contains an extension since the . is not a valid character in the FROM clause. Try FROM [{0}] (though this may not be the correct quoting character for the OleDbDataAdapter).

How to give path of service based database in C#

I have added a service based database(.mdf),to my Window form application project.But I
am not able to give the right path of the database. I am giving the path as follows but it
gives an exception that unable to open the connection
connection = new SqlConnection("user id=Sarao-PC\\Sarao;" +
"password=sarao;server=SARAO-PC\\SQLEXPRESS" +
"Trusted_Connection=yes;" +
"database=Database11 " +
"connection timeout=30");
Database11 is a service based database .
What is the right way to give path of a .mdf file
Why don't you copy it from properties of your database?
goto view tab, select server explorer. it opens up on your left, and you should see your database there, then just right click>properties. and there copy the text from the field 'Connection String'.
You should write like below:
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
First, you need to build the path to the file.
So how do you package the mdf file ? If you put it in your VS project, then you set Build action to "Content" you'll have the file in the same directory than you exe.
So to build the path it'll be easy:
string mdfPath = Path.Combine(Application.StartupPath, "MyDb.mdf");
Then just create the connection string using this path:
connection = new SqlConnection("user id=Sarao-PC\\Sarao;" +
"password=sarao;" +
"Trusted_Connection=yes;" +
"database=Database11;" +
"AttachDbFilename=" + mdfPath + ";");
Standard Security Connection strings
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Mixed Data Type in Excel, IMEX Doesn't Work

I am trying to export xls into datatable. Below is my connection string.
string path = //xls source path
OleDbConnection MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties='Excel8.0;IMEX=1;TypeGuessRows=0;HDR=No;ImportMixedTypes=Text'");
I set IMEX=1 and all the other extended properties as I have to deal with mixed datatypes.
Even though I set the connection like that yet I still produced error.
There are no error messages, but the inconsistent rows (who don't follow the majority datatype are set to null instead).
Can someone tell me what did I miss? Btw, I am using the OleDbDataAdapter & Fill(DataSet) method.
Are you sure that TypeGuessRows=0; and ImportMixedTypes=Text; are working from connection string and should not be modified in registry (HKEY_LOCAL_MACHINE\SOFTWARE\[Wow6432Node\]Microsoft\Jet\4.0\Engines\Excel)? AFAIK this settings are read from registry. ImportMixedTypes=Text is usually by default, but TypeGuessRows=8, and should be set to 0 as in your connection string.

Categories