The problem i’m having is that the data adapter is looking at only the first row in each column to determine the data type. In my case the first column «SKU» is numbers for the first 500 rows then I happen to have SKU’s which are mixed numbers and letters. So what ends up happening is rows in the SKU column are left blank, but I still get the other information for each column row.
I believe it is the connection string that controls that and with my current settings it should work, however it is not.
Connection String:
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersNickDesktopPricing2.xlsx" + @";Extended Properties=""Excel 12.0 Xml;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
ImportMixedTypes=Text;TypeGuessRows=0
Should be the important keywords, look at 0 rows and just use text as the value types for everything.
The «bandaid» I have put on this is to make the first row in the spreadsheet a mixture of letters and numbers and specifically leave that row out in my query.
asked Dec 29, 2010 at 3:12
The Muffin ManThe Muffin Man
19.4k30 gold badges119 silver badges190 bronze badges
3
Unfortunately, you can’t set ImportMixedTypes
or TypeGuessRows
from the connection string since those settings are defined in the registry. For the ACE OleDb driver, they’re stored at
HKEY_LOCAL_MACHINESOFTWAREMicrosoftOffice14.0Access Connectivity EngineEnginesExcel
in the registry. So, you can simplify your connection string to:
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersNickDesktopPricing2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes;IMEX=1;""";
Once you set TypeGuessRows
to 0
and ImportMixedTypes
to Text
in the registry, you should get the behavior you are expecting. You might, however, consider using a suitably large number like 1000 instead of zero if you find import performance to be less than ideal.
answered Jan 11, 2011 at 23:02
arcainarcain
14.8k6 gold badges55 silver badges75 bronze badges
8
This reference section describes additional connection string information when using EDT to load data directly from an Excel spreadsheet file.
The Excel Database Tasks (EDT) software can load data from ANY source either as an Excel report, or Validate and send the data to any destination Table or Stored Procedure. Supporting MS SQL Server, Oracle, MySQL, Access, DB2 databases. |
Download EDT Free Trial |
A connection string can be pasted into the EDT Data Source connection string text box as highlighted below.
After modifying the connection string, click the Test button to verify the connection:
Microsoft ACE OLEDB 12.0
Microsoft ACE driver will allow you to query Office files (Including Access database AND Excel files)
ACE driver is available from Microsoft here:
Xlsx files
Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;
Extended Properties=»Excel 12.0 Xml;HDR=YES»;
«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.
Treating data as text
Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsx;
Extended Properties=»Excel 12.0 Xml;HDR=YES;IMEX=1″;
If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.
Xlsb files
Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=c:myFoldermyBinaryExcel2007file.xlsb;
Extended Properties=»Excel 12.0;HDR=YES»;
You can also use this connection string to connect to older 97-2003 Excel workbooks.»HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.
Xlsm files
Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyExcel2007file.xlsm;
Extended Properties=»Excel 12.0 Macro;HDR=YES»;
«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.
Excel 97-2003 Xls files with ACE OLEDB 12.0
You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:myFoldermyOldExcelFile.xls;
Extended Properties=»Excel 8.0;HDR=YES»;
«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.
Microsoft Jet OLE DB 4.0
Standard (Excel)
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:MyExcel.xls;
Extended Properties=»Excel 8.0;HDR=Yes;IMEX=1″;
How to Use JET in 64 bit environments
Standard alternative
Try this one if the one above is not working. Some reports that Excel 2003 need the exta OLEDB; section in the beginning of the string.
OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:MyExcel.xls;Extended Properties=»Excel 8.0;HDR=Yes;IMEX=1″;
«HDR=Yes;» indicates that the first row contains column names, not data. «HDR=No;» indicates the opposite.»IMEX=1;» tells the driver to always read «intermixed» (numbers, dates, strings etc) data columns as text. Note that this option might affect excel sheet write access negative.SQL syntax «SELECT [Column Name One], [Column Name Two] FROM [Sheet One$]». I.e. excel worksheet name followed by a «$» and wrapped in «[» «]» brackets.Check out the [HKEY_LOCAL_MACHINESOFTWAREMicrosoftJet4.0EnginesExcel] located registry REG_DWORD «TypeGuessRows». That’s the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows. This might hurt performance. Please also note that adding the IMEX=1 option might cause the IMEX feature to set in after just 8 rows. Use IMEX=0 instead to be sure to force the registry TypeGuessRows=0 (scan all rows) to work.If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: «Could not decrypt file.»
.NET Framework Data Provider for OLE DB
Use an OLE DB provider from .NET
Provider=any oledb provider’s name;OledbKey1=someValue;OledbKey2=someValue;
See the respective OLEDB provider’s connection strings options. The .net OleDbConnection will just pass on the connection string to the specified OLEDB provider.
Microsoft Excel 2007 ODBC Driver
Standard
Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
DBQ=C:MyExcel.xlsx;
Standard (for versions 97 — 2003)
Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
DBQ=C:MyExcel.xls;
Standard
Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=C:MyExcel.xls;
DefaultDir=c:mypath;
SQL syntax «SELECT [Column Name One], [Column Name Two] FROM [Sheet One$]». I.e. excel worksheet name followed by a «$» and wrapped in «[» «]» brackets.
Specify ReadOnly
[Microsoft][ODBC Excel Driver] Operation must use an updateable query. Use this connection string to avoid the error.
Driver={Microsoft Excel Driver (*.xls)};Dbq=C:MyExcel.xls;ReadOnly=0;
ReadOnly = 0 specifies the connection to be updateable.
.NET Framework Data Provider for ODBC
Use an ODBC driver from .NET
Driver={any odbc driver’s name};OdbcKey1=someValue;OdbcKey2=someValue;
See the respective ODBC driver’s connection strings options. The .net Odbc Connection will just pass on the connection string to the specified ODBC driver.
.NET xlReader for Microsoft Excel
Excel file with header row
Data Source =c:myExcelFile.xlsx;HDR=yes;Format=xlsx;
Excel file without header row
Data Source =c:myExcelFile.xlsx;HDR=no;Format=xlsx;
Excel file with header row (for versions 97 — 2003)
Data Source =c:myExcelFile.xls;HDR=yes;Format=xls;
Excel file without header row (for versions 97 — 2003)
Data Source =c:myExcelFile.xls;HDR=no;Format=xls;
RSSBus ADO.NET Provider for Excel
Standard
Excel File=C:myExcelFile.xlsx;
No headers in Excel sheet
Excel File=C:myExcelFile.xlsx;Header=False;
Pseudo column names (A,B,C) are used instead.
Caching data
Excel File=C:myExcelFile.xlsx;Cache Location=C:cache.db;Auto Cache=true;
Offline=false;
To retrieve data from the cache, add «#Cache» to the table name. For example, to query cached data from the «Sheet» table, execute «SELECT * FROM [Sheet#Cache]».
Caching data and metadata
Excel File=C:myExcelFile.xlsx;Cache Location=C:cache.db;Auto Cache=true;
Offline=false;Cache Metadata=true;
The table metadata will also be cached instead of retrieving it from the data source. This improves connection performance.
Cached data only / Offline mode
Excel File=C:myExcelFile.xlsx;Offline=true;Query Passthrough=true;
Cache Location=C:cache.db;
SELECT statements will always retrieve data from the cache. DELETE/UPDATE/INSERT statements is not allowed and will throw an exception. Excel 2000Excel 2002Excel 2003Excel 2007Excel 2010Excel 2013Excel 97
Using an External Cache Provider
RSSBus drivers have the ability to cache data in a separate database such as SQL Server or MySQL instead of in a local file using the following syntax:
Cache Provider=Provider.Namespace;
Cache Connection=’Connection String to Cache Database’;
Above is just an example to show how it works. It can be used both with «Auto Cache» and with «Cached Data Only / Offline Mode».
Empty cells always NULL
Excel File=C:myExcelFile.xlsx;Empty Text Mode=EmptyAsNull;
Empty cells always empty string
Excel File=C:myExcelFile.xlsx;Empty Text Mode=NullAsEmpty;
Suppress formula calculation errors
Excel File=C:myExcelFile.xlsx;Ignore Calc Error=true;
Read «tilted sheets», where rows are headers and columns are rows
Excel File=C:myExcelFile.xlsx;Orientation=Horizontal;
Do not use formulas, only values
Do not treat values starting with equals (=) as formulas during inserts and updates.
Excel File=C:myExcelFile.xlsx;Allow Formula=false;
- Remove From My Forums
Using OleDb to read Excel 2007 xls file — connection string
-
Question
-
I wrote a windows form application in C# that uses ADO.Net to read from an Excel file. I receive xls files but have to manually save them as xlsx files because I can’t get the program to read xls files (I get «External table is not in the expected format»). As an aside, for broadest .net compatibility, I set each of the Projects (which create assemblies/dlls) for the application to .net framework 2.0.
I have 2 questions:
1. How can I get my program to read xls files when Excel 2007 is installed?
2. How can I support environments where Excel 2002 and Excel 2003 are installed — I assume different connection strings and have the users ONLY open xls files?
Code Snippet:
DbProviderFactory factory = DbProviderFactories.GetFactory(«System.Data.OleDb»);
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = «whatever»;
connection.Open();
Connection strings I’ve tried:
// The first one worked for xlsx files w/ Excel 2007 installed:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFile.xls;Extended Properties=»»Excel 12.0;IMEX=1;HDR=YES;»»
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myFile.xls;Extended Properties=»»Excel 8.0;HDR=Yes;IMEX=1;»»
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=myFile.xls;Extended Properties=»»Excel 5.0;HDR=Yes;IMEX=1;»»
Answers
-
If you want to read xlsx files, you need to use ACE OLEDB provider (first connection string). You do not have to install Excel to use it, but since you installed Excel it also installed provider for you. I believe you can install ACE as part of SDK as well and avoid installing Excel application. Jet is obsolete now and is replaced by ACE. Basically you can use first connection string for XLSX or XLS files, but you need to specify Excel 8.0 for XLS and Excel 12.0 for XLSX files, which means you would need to build connection string dynamically. You also need to specify correct extension for you files — if it is xlsx than you should put it instead of xlsx. You connection string for XLSX files would be something like
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFile.xlsx;Extended Properties=»»Excel 12.0 Xml;IMEX=1;HDR=YES;»»
and for XLS files
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFile.xls;Extended Properties=»»Excel 8.0;IMEX=1;HDR=YES;»»
If you need .NET managed provider for Excel and planning only to read data (no updates), you can try .NET reader for Excel from my web site.
Val Mazur (MVP)
http://www.xporttools.net
-
Marked as answer by
Wednesday, March 25, 2009 9:52 PM
-
Marked as answer by
Содержание
- Connection String Syntax
- Connection String Builders
- Windows Authentication
- SqlClient Connection Strings
- Windows authentication with SqlClient
- SQL Server authentication with SqlClient
- Connect to a named instance of SQL Server
- Type System Version Changes
- Connecting and Attaching to SQL Server Express User Instances
- Using TrustServerCertificate
- Enabling Encryption
- OleDb Connection Strings
- OleDb Connection String Syntax
- Using DataDirectory to Connect to Access/Jet
- Connecting to Excel
- Data Shape Provider Connection String Syntax
- Odbc Connection Strings
- Using DataDirectory to Connect to Visual FoxPro
- Oracle Connection Strings
- Excel connection strings
- .NET libraries
- OLE DB providers
- ODBC drivers
- Wrappers and others
- CData ADO.NET Provider for Excel
- Standard
- No headers in Excel sheet
- Caching data
- Caching data and metadata
- Cached data only / Offline mode
- Using an External Cache Provider
- Empty cells always NULL
- Empty cells always empty string
- Suppress formula calculation errors
- Read «tilted sheets», where rows are headers and columns are rows
- Do not use formulas, only values
- Microsoft ACE OLEDB 12.0
- Xlsx files
- Treating data as text
- Xlsb files
- Xlsm files
- Excel 97-2003 Xls files with ACE OLEDB 12.0
- Microsoft Jet OLE DB 4.0
- Standard (Excel)
- Standard alternative
- .NET Framework Data Provider for OLE DB
- Use an OLE DB provider from .NET
Connection String Syntax
Each .NET Framework data provider has a Connection object that inherits from DbConnection as well as a provider-specific ConnectionString property. The specific connection string syntax for each provider is documented in its ConnectionString property. The following table lists the four data providers that are included in the .NET Framework.
.NET Framework data provider | Description |
---|---|
System.Data.SqlClient | Provides data access for Microsoft SQL Server. For more information on connection string syntax, see ConnectionString. |
System.Data.OleDb | Provides data access for data sources exposed using OLE DB. For more information on connection string syntax, see ConnectionString. |
System.Data.Odbc | Provides data access for data sources exposed using ODBC. For more information on connection string syntax, see ConnectionString. |
System.Data.OracleClient | Provides data access for Oracle version 8.1.7 or later. For more information on connection string syntax, see ConnectionString. |
Connection String Builders
ADO.NET 2.0 introduced the following connection string builders for the .NET Framework data providers.
The connection string builders allow you to construct syntactically valid connection strings at run time, so you do not have to manually concatenate connection string values in your code. For more information, see Connection String Builders.
Windows Authentication
We recommend using Windows Authentication (sometimes referred to as integrated security) to connect to data sources that support it. The syntax employed in the connection string varies by provider. The following table shows the Windows Authentication syntax used with the .NET Framework data providers.
Provider | Syntax |
---|---|
SqlClient | Integrated Security=true;
Integrated Security=SSPI; |
OleDb | Integrated Security=SSPI; |
Odbc | Trusted_Connection=yes; |
OracleClient | Integrated Security=yes; |
Integrated Security=true throws an exception when used with the OleDb provider.
SqlClient Connection Strings
The syntax for a SqlConnection connection string is documented in the SqlConnection.ConnectionString property. You can use the ConnectionString property to get or set a connection string for a SQL Server database. If you need to connect to an earlier version of SQL Server, you must use the .NET Framework Data Provider for OleDb (System.Data.OleDb). Most connection string keywords also map to properties in the SqlConnectionStringBuilder.
The default setting for the Persist Security Info keyword is false . Setting it to true or yes allows security-sensitive information, including the user ID and password, to be obtained from the connection after the connection has been opened. Keep Persist Security Info set to false to ensure that an untrusted source does not have access to sensitive connection string information.
Windows authentication with SqlClient
Each of the following forms of syntax uses Windows Authentication to connect to the AdventureWorks database on a local server.
SQL Server authentication with SqlClient
Windows Authentication is preferred for connecting to SQL Server. However, if SQL Server Authentication is required, use the following syntax to specify a user name and password. In this example, asterisks are used to represent a valid user name and password.
When you connect to Azure SQL Database or to Azure SQL Data Warehouse and provide a login in the format user@servername , make sure that the servername value in the login matches the value provided for Server= .
Windows authentication takes precedence over SQL Server logins. If you specify both Integrated Security=true as well as a user name and password, the user name and password will be ignored and Windows authentication will be used.
Connect to a named instance of SQL Server
To connect to a named instance of SQL Server, use the server nameinstance name syntax.
You can also set the DataSource property of the SqlConnectionStringBuilder to the instance name when building a connection string. The DataSource property of a SqlConnection object is read-only.
Type System Version Changes
The Type System Version keyword in a SqlConnection.ConnectionString specifies the client-side representation of SQL Server types. See SqlConnection.ConnectionString for more information about the Type System Version keyword.
Connecting and Attaching to SQL Server Express User Instances
User instances are a feature in SQL Server Express. They allow a user running on a least-privileged local Windows account to attach and run a SQL Server database without requiring administrative privileges. A user instance executes with the user’s Windows credentials, not as a service.
For more information on working with user instances, see SQL Server Express User Instances.
Using TrustServerCertificate
The TrustServerCertificate keyword is valid only when connecting to a SQL Server instance with a valid certificate. When TrustServerCertificate is set to true , the transport layer will use SSL to encrypt the channel and bypass walking the certificate chain to validate trust.
If TrustServerCertificate is set to true and encryption is turned on, the encryption level specified on the server will be used even if Encrypt is set to false in the connection string. The connection will fail otherwise.
Enabling Encryption
To enable encryption when a certificate has not been provisioned on the server, the Force Protocol Encryption and the Trust Server Certificate options must be set in SQL Server Configuration Manager. In this case, encryption will use a self-signed server certificate without validation if no verifiable certificate has been provisioned on the server.
Application settings cannot reduce the level of security configured in SQL Server, but can optionally strengthen it. An application can request encryption by setting the TrustServerCertificate and Encrypt keywords to true , guaranteeing that encryption takes place even when a server certificate has not been provisioned and Force Protocol Encryption has not been configured for the client. However, if TrustServerCertificate is not enabled in the client configuration, a provisioned server certificate is still required.
The following table describes all cases.
Force Protocol Encryption client setting | Trust Server Certificate client setting | Encrypt/Use Encryption for Data connection string/attribute | Trust Server Certificate connection string/attribute | Result |
---|---|---|---|---|
No | N/A | No (default) | Ignored | No encryption occurs. |
No | N/A | Yes | No (default) | Encryption occurs only if there is a verifiable server certificate, otherwise the connection attempt fails. |
No | N/A | Yes | Yes | Encryption always occurs, but may use a self-signed server certificate. |
Yes | No | Ignored | Ignored | Encryption occurs only if there is a verifiable server certificate; otherwise, the connection attempt fails. |
Yes | Yes | No (default) | Ignored | Encryption always occurs, but may use a self-signed server certificate. |
Yes | Yes | Yes | No (default) | Encryption occurs only if there is a verifiable server certificate; otherwise, the connection attempt fails. |
Yes | Yes | Yes | Yes | Encryption always occurs, but may use a self-signed server certificate. |
OleDb Connection Strings
The ConnectionString property of a OleDbConnection allows you to get or set a connection string for an OLE DB data source, such as Microsoft Access. You can also create an OleDb connection string at run time by using the OleDbConnectionStringBuilder class.
OleDb Connection String Syntax
You must specify a provider name for an OleDbConnection connection string. The following connection string connects to a Microsoft Access database using the Jet provider. Note that the User ID and Password keywords are optional if the database is unsecured (the default).
If the Jet database is secured using user-level security, you must provide the location of the workgroup information file (.mdw). The workgroup information file is used to validate the credentials presented in the connection string.
It is possible to supply connection information for an OleDbConnection in a Universal Data Link (UDL) file; however you should avoid doing so. UDL files are not encrypted, and expose connection string information in clear text. Because a UDL file is an external file-based resource to your application, it cannot be secured using the .NET Framework. UDL files are not supported for SqlClient.
Using DataDirectory to Connect to Access/Jet
DataDirectory is not exclusive to SqlClient . It can also be used with the System.Data.OleDb and System.Data.Odbc .NET data providers. The following sample OleDbConnection string demonstrates the syntax required to connect to the Northwind.mdb located in the application’s app_data folder. The system database (System.mdw) is also stored in that location.
Specifying the location of the system database in the connection string is not required if the Access/Jet database is unsecured. Security is off by default, with all users connecting as the built-in Admin user with a blank password. Even when user-level security is correctly implemented, a Jet database remains vulnerable to attack. Therefore, storing sensitive information in an Access/Jet database is not recommended because of the inherent weakness of its file-based security scheme.
Connecting to Excel
The Microsoft Jet provider is used to connect to an Excel workbook. In the following connection string, the Extended Properties keyword sets properties that are specific to Excel. «HDR=Yes;» indicates that the first row contains column names, not data, and «IMEX=1;» tells the driver to always read «intermixed» data columns as text.
Note that the double quotation character required for the Extended Properties must also be enclosed in double quotation marks.
Data Shape Provider Connection String Syntax
Use both the Provider and the Data Provider keywords when using the Microsoft Data Shape provider. The following example uses the Shape provider to connect to a local instance of SQL Server.
Odbc Connection Strings
The ConnectionString property of a OdbcConnection allows you to get or set a connection string for an OLE DB data source. Odbc connection strings are also supported by the OdbcConnectionStringBuilder.
The following connection string uses the Microsoft Text Driver.
Using DataDirectory to Connect to Visual FoxPro
The following OdbcConnection connection string sample demonstrates using DataDirectory to connect to a Microsoft Visual FoxPro file.
Oracle Connection Strings
The ConnectionString property of a OracleConnection allows you to get or set a connection string for an OLE DB data source. Oracle connection strings are also supported by the OracleConnectionStringBuilder .
For more information on ODBC connection string syntax, see ConnectionString.
Источник
Excel connection strings
.NET libraries
OLE DB providers
ODBC drivers
Wrappers and others
CData ADO.NET Provider for Excel
Standard
Pseudo column names (A,B,C) are used instead.
Caching data
To retrieve data from the cache, add «#Cache» to the table name. For example, to query cached data from the «Sheet» table, execute «SELECT * FROM [Sheet#Cache]».
Caching data and metadata
The table metadata will also be cached instead of retrieving it from the data source. This improves connection performance. Read more here →
Cached data only / Offline mode
SELECT statements will always retrieve data from the cache. DELETE/UPDATE/INSERT statements is not allowed and will throw an exception.
Using an External Cache Provider
RSSBus drivers have the ability to cache data in a separate database such as SQL Server or MySQL instead of in a local file using the following syntax:
Cache Provider = Provider.Namespace; Cache Connection = ‘Connection String to Cache Database’;
Above is just an example to show how it works. It can be used both with «Auto Cache» and with «Cached Data Only / Offline Mode». Read more about using RSSBus Cache Provider in this article >>>
Empty cells always NULL
Empty cells always empty string
Suppress formula calculation errors
Do not use formulas, only values
Do not treat values starting with equals (=) as formulas during inserts and updates.
Excel File = C:myExcelFile.xlsx; Allow Formula = false;
Microsoft ACE OLEDB 12.0
Xlsx files
Connect to Excel 2007 (and later) files with the Xlsx file extension. That is the Office Open XML format with macros disabled.
Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES»;
«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.
Treating data as text
Use this one when you want to treat all data in the file as text, overriding Excels column type «General» to guess what type of data is in the column.
Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsx; Extended Properties = «Excel 12.0 Xml; HDR = YES; IMEX = 1»;
If you want to read the column headers into the result set (using HDR=NO even though there is a header) and the column data is numeric, use IMEX=1 to avoid crash.
To always use IMEX=1 is a safer way to retrieve data for mixed data columns. Consider the scenario that one Excel file might work fine cause that file’s data causes the driver to guess one data type while another file, containing other data, causes the driver to guess another data type. This can cause your app to crash.
Xlsb files
Connect to Excel 2007 (and later) files with the Xlsb file extension. That is the Office Open XML format saved in a binary format. I e the structure is similar but it’s not saved in a text readable format as the Xlsx files and can improve performance if the file contains a lot of data.
Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyBinaryExcel2007file.xlsb; Extended Properties = «Excel 12.0; HDR = YES»;
You can also use this connection string to connect to older 97-2003 Excel workbooks.
«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.
Xlsm files
Connect to Excel 2007 (and later) files with the Xlsm file extension. That is the Office Open XML format with macros enabled.
Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyExcel2007file.xlsm; Extended Properties = «Excel 12.0 Macro; HDR = YES»;
«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.
Excel 97-2003 Xls files with ACE OLEDB 12.0
You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.
Provider = Microsoft.ACE.OLEDB.12.0; Data Source = c:myFoldermyOldExcelFile.xls; Extended Properties = «Excel 8.0; HDR = YES»;
«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.
Microsoft Jet OLE DB 4.0
Standard (Excel)
Standard alternative
Try this one if the one above is not working. Some reports that Excel 2003 need the exta OLEDB; section in the beginning of the string.
OLEDB; Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:MyExcel.xls; Extended Properties = «Excel 8.0; HDR = Yes; IMEX = 1»;
«HDR=Yes;» indicates that the first row contains columnnames, not data. «HDR=No;» indicates the opposite.
«IMEX=1;» tells the driver to always read «intermixed» (numbers, dates, strings etc) data columns as text. Note that this option might affect excel sheet write access negative.
SQL syntax «SELECT [Column Name One], [Column Name Two] FROM [Sheet One$]». I.e. excel worksheet name followed by a «$» and wrapped in «[» «]» brackets.
«SELECT * FROM [Sheet1$a5:d]», start picking the data as of row 5 and up to column D.
Check out the [HKEY_LOCAL_MACHINESOFTWAREMicrosoftJet4.0EnginesExcel] located registry REG_DWORD «TypeGuessRows». That’s the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows. This might hurt performance. Please also note that adding the IMEX=1 option might cause the IMEX feature to set in after just 8 rows. Use IMEX=0 instead to be sure to force the registry TypeGuessRows=0 (scan all rows) to work.
If the Excel workbook is protected by a password, you cannot open it for data access, even by supplying the correct password with your connection string. If you try, you receive the following error message: «Could not decrypt file.»
.NET Framework Data Provider for OLE DB
Use an OLE DB provider from .NET
See the respective OLEDB provider’s connection strings options. The .net OleDbConnection will just pass on the connection string to the specified OLEDB provider. Read more here.
Источник
It’s hard to remember details of database connection strings.
Here is an easy reference for the most popular databases.
Note: Code samples are in C#. You must provide the values in red
Microsoft SQL Server
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Standard Connection
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server};" + "Server=DataBaseNamex;" + "DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Trusted Connection
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server};" + "Server=ServerName;" + "DataBase=DataBaseName;" + "Uid=;" + "Pwd=;"; conn.Open(); // or var conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server};" + "Server=ServerName;" + "DataBase=DataBaseName;" + "Trusted_Connection=Yes;"; conn.Open();
OleDb — Standard Connection
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB;" + "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
OleDb — Trusted Connection
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB;" + "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "Integrated Security=SSPI;"; conn.Open();
OleDb — via IP Address
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB;" + "Network Library=DBMSSOCN;" + "Data Source=xxx.xxx.xxx.xxx,1433;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
.NET DataProvider — Standard Connection
using System.Data.SqlClient; var conn = new SqlDbConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
.NET DataProvider — Trusted Connection
using System.Data.SqlClient; var conn = new SqlConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "Integrated Security=SSPI;"; conn.Open();
.NET DataProvider — via IP Address
using System.Data.SqlClient; var conn = new SqlConnection(); conn.ConnectionString = "Network Library=DBMSSOCN;" + "Data Source=xxx.xxx.xxx.xxx,1433;" + "Initial Catalog=DataBaseName;" + "User Id=UserName;" + "Password=Secret;"; conn.Open();
Microsoft Sql Express
.NET Data Provider — Default Relative Path — Standard Connection
using System.Data.SqlClient; var conn = new SqlConnection(); conn.ConnectionString = "Data Source=.SQLExpress;" + "User Instance=true;" + "User Id=UserName;" + "Password=Secret;" + "AttachDbFilename=|DataDirectory|DataBaseName.mdf;" conn.Open();
.NET Data Provider — Default Relative Path — Trusted Connection
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();
.NET Data Provider — Custom Relative Path — Standard Connection
using System.Data.SqlClient; AppDomain.CurrentDomain.SetData( "DataDirectory", "C:MyPath"); var conn = new SqlConnection(); conn.ConnectionString = "Data Source=.SQLExpress;" + "User Instance=true;" + "User Id=UserName;" + "Password=Secret;" + "AttachDbFilename=|DataDirectory|DataBaseName.mdf;" conn.Open();
.NET Data Provider — Custom Relative Path — Trusted Connection
using System.Data.SqlClient; AppDomain.CurrentDomain.SetData( "DataDirectory", "C:MyPath"); var conn = new SqlConnection(); conn.ConnectionString = "Data Source=.SQLExpress;" + "User Instance=true;" + "Integrated Security=true;" + "AttachDbFilename=|DataDirectory|DataBaseName.mdf;" conn.Open();
.NET Data Provider — Absolute Path — Standard Connection
using System.Data.SqlClient; var conn = new SqlConnection(); conn.ConnectionString = "Data Source=.SQLExpress;" + "User Instance=true;" + "User Id=UserName;" + "Password=Secret;" + "AttachDbFilename=C:MyPathDataBaseName.mdf;" conn.Open();
.NET Data Provider — Absolute Path — Trusted Connection
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=C:MyPathDataBaseName.mdf;"
conn.Open();
Microsoft Access
ODBC DSN
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = "Dsn=DsnName";
conn.Open();
ODBC — Standard Security
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={Microsoft Access Driver (*.mdb)};" +
"Dbq=c:myPathmyDb.mdb;" +
"Uid=Admin;Pwd=;";
conn.Open();
ODBC — Workgroup (System Database)
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" + "Dbq=c:myPathmyDb.mdb;" + "SystemDb=c:myPathmyDb.mdw;"; conn.Open();
ODBC — Exclusive Use
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={Microsoft Access Driver (*.mdb)};" +
"Dbq=c:myPathmyDb.mdb;" +
"Exclusive=1;";
"Uid=Admin;Pwd=;";
conn.Open();
OleDb with MS Jet — Standard Security
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:mypathmyDb.mdb;" + "User id=admin;" + "Password="; conn.Open();
OleDb with MS Jet — Workgroup (System Database)
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:mypathmyDb.mdb;" + "System Database=c:mypathmyDb.mdw;"; conn.Open();
OleDb with MS Jet — With Password
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:mypathmyDb.mdb;" + "Database Password=Secret;" conn.Open();
ORACLE
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — New Microsoft Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft ODBC for Oracle};" + "Server=OracleServer.world;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Old Microsoft Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft ODBC Driver for Oracle};" + "ConnectString=OracleServer.world;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Oracle Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Oracle ODBC Driver};" + "Dbq=myDataBase;" + // define in tsnames.ora "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
OleDb — Microsoft Driver
uusing System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=MSDAORA;" + "Data Source=ServerName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
OleDb — Oracle Driver — Standard Connection
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=OraOLEDB.Oracle;" + "Data Source=ServerName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
OleDb — Oracle Driver — Trusted Connection
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=OraOLEDB.Oracle;" + "Data Source=ServerName;" + "OSAuthent=1;"; conn.Open(); // or using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=OraOLEDB.Oracle;" + "Data Source=ServerName;" + "User id=/" + "Password=;"; conn.Open();
.NET DataProvider from Microsoft — Standard Connection
using System.Data.OracleClient; var conn = new OracleConnection(); conn.ConnectionString = "Data Source=ServerName;" + "User id=UserName;"; "Password=Secret;"; conn.Open();
.NET DataProvider from Microsoft — Trusted Connection
using System.Data.OracleClient;
var conn = new OracleConnection();
conn.ConnectionString =
"Data Source=Servername;" +
"Integrated Security=Yes;";
conn.Open();
.NET DataProvider from Oracle — Standard Connection
using Oracle.DataAccess.Client; var conn = new OracleConnection(); conn.ConnectionString = "Data Source=ServerName;" + "User id=UserName;"; "Password=Secret;"; conn.Open();
.NET DataProvider from Oracle — Trusted Connection
using Oracle.DataAccess.Client;
var conn = new OracleConnection();
conn.ConnectionString =
"Data Source=Servername;" +
"Integrated Security=Yes;";
conn.Open();
IBM DB2
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC without DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={IBM DB2 ODBC DRIVER};" + "DataBase=DataBaseName;" + "HostName=ServerName;" + "Protocol=TCPIP;" + "Port=PortNumber;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
OleDb — Microsoft Driver
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=DB2OLEDB;" + "Network Transport Library=TCPIP;" + "Network Address=xxx.xxx.xxx.xxx;" + "Package Collection=CollectionName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
OleDb — IBM Driver
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=IBMDADB2;" + "DataBase=DataBaseName;" + "HostName=ServerName;" + "Protocol=TCPIP;" + "Port=PortNumber;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
.NET DataProvider from IBM
using IBM.Data.DB2; var conn = new Db2Connection(); conn.ConnectionString = "DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;" + conn.Open();
MySql
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — MyODBC Driver — local database
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={MySql};" +
"Server=localhost;" +
"Option=16834;" +
"DataBase=DataBaseName;"
conn.Open();
ODBC — MyODBC Driver — remote database
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={MySql};" + "Server=db.domain.com;" + "Option=131072;" + "Port=3306;" + "Stmt=;" + "DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;" conn.Open();
ODBC — MySQL ODBC 3.51 Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={MySql ODBC 3.51 Driver};" + "Server=ServerName;" + "Option=16834;" + "Port=3306;" + "Stmt=;" + "DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;" conn.Open(); // or var conn = new OdbcConnection(); conn.ConnectionString = "DRIVER={MySql ODBC 3.51 Driver};" + "SERVER=ServerName;" + "DATABASE=DataBaseName;" + "USER=UserName;" + "PASSWORD=Secret;" conn.Open();
OleDb
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Provider=MySqlProv;" + "Data Source=ServerName;" + "User id=UserName;" + "Password=Secret;" conn.Open();
.NET DataProvider from CoreLab
using CoreLab.MySql; var conn = new MySqlConnection(); conn.ConnectionString = "Host=ServerName;" + "DataBase=DataBaseName;" + "Protocol=TCP;" + "Port=3306;" + "Direct=true;" + "Compress=false;" + "Pooling=true;" + "Min Pool Size=0;" + "Max Pool Size=100;" + "Connection Lifetime=0;" + "User id=UserName;" + "Password=Secret;" + conn.Open();
Sybase
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Sybase System 12 (12.5) ODBC Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={SYBASE ASE ODBC Driver};" + "Srvr=ServerName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Sybase System 11 ODBC Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={SYBASE SYSTEM 11};" + "Srvr=ServerName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Intersolv 3.10 ODBC Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={INTERSOLV 3.10 32-BIT Sybase};" + "Srvr=ServerName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — SQL Anywhere
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "ODBC;" + "Driver={Sybase SQL Anywhere 5.0};" + "DefaultDir=c:myfolder;" + "Dbf=c:mypathdbname.db;" + "Uid=UserName;" + "Pwd=Secret;" + "Dsn="""";"; // Must be included! conn.Open();
OleDb — Sybase Adaptive Server Enterprise (ASE)
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=Sybase.ASEOLEDBProvider;" + "Server Name=ServerName,5000;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open(); // optionally, replace // 'Server Name' with 'Srvr', and // 'Initial Catalog' with 'Catalog'
.NET DataProvider from Sybase
using Sybase.Data.AseClient; var conn = new AseConnection(); conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=Secret;"; conn.Open();
Interbase
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — EasySoft ODBC Driver — local machine
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Easysoft IB6 ODBC};" + "Server=localhost;" + "DataBase=localhost:C:MyPathDbName.gdb;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — EasySoft ODBC Driver — remote machine
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Easysoft IB6 ODBC};" + "Server=ServerName;" + "DataBase=ServerName:C:MyPathDbName.gdb;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Intersolv ODBC Driver — local machine
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver=" + "{INTERSOLV InterBase ODBC Driver (*.gdb)};" + "Server=localhost;" + "DataBase=localhost:C:MyPathDbName.gdb;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC — Intersolv ODBC Driver — remote machine
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver=" + "{INTERSOLV InterBase ODBC Driver (*.gdb)};" + "Server=ServerName;" + "DataBase=ServerName:C:MyPathDbName.gdb;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
Informix
ODBC DSN — INFORMIX 3.30 ODBC Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Host=HostName;" + "Server=ServerName;" + "Service=ServerName;" + "Protocol=olsoctcp;" + "Database=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC without DSN — INFORMIX 3.30 ODBC Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn="";" + "Driver={INFORMIX 3.30 32 BIT};" + "Host=HostName;" + "Server=ServerName;" + "Service=ServerName;" + "Protocol=olsoctcp;" + "Database=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC Informix-CLI 2.5 ODBC Driver
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Informix-CLI 2.5 (32 Bit)};" + "Server=ServerName;" + "DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
OleDb — IBM Informix OleDb Provider
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=IFXOLEDBC;" + "Data Source=DataBaseName@ServerName;" + "User id=UserName;" + "Password=Secret;"; "Persist Security Info=true;"; conn.Open();
Excel
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC without DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" + "Driverid=790;" + "Dbq=C:MyPathSpreadSheet.xls;" + "DefaultDir=C:MyPath;"; conn.Open();
OleDb with MS Jet
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:MyPathSpreadSheet.xls;" + @"Extended Properties=""Excel 8.0;HDR=Yes"""; conn.Open();
<
Text
ODBC DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Dsn=DsnName;" + "Uid=UserName;" + "Pwd=Secret;"; conn.Open();
ODBC without DSN
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};" + "Dbq=C:MyPath;" + "Extensions=asc,csv,tab,txt;"; conn.Open(); // Use: sql = "Select * From MyTextFile.txt"
OleDb with MS Jet
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:MyPath;" + "Extended Properties=" + @"""text;HDR=Yes;FMT=Delimited"""; conn.Open(); // Use: sql = "Select * From MyTextFile.txt"
dBase Dbf
ODBC DSN
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = "Dsn=DsnName";
conn.Open();
// Use: sql = "Select * From MyDb.dbf"
ODBC without DSN
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={Microsoft dBASE Driver (*.dbf)};" +
"Driverid=277;" +
"Dbq=C:MyPath";
conn.Open();
// Use: sql = "Select * From MyDb.dbf"
Visual FoxPro
ODBC DSN
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = "Dsn=DsnName";
conn.Open();
ODBC without DSN — Database container (dbc)
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Visual FoxPro Driver};" + "SourceType=DBC;" + "SourceDB=C:MyPathMyDb.dbc;" + "Exclusive=No"; conn.Open();
ODBC without DSN — Free table directory
using System.Data.Odbc; var conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Visual FoxPro Driver};" + "SourceType=DBF;" + "SourceDB=C:MyPath;" + "Exclusive=No"; conn.Open();
OleDb — Database container (dbc)
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=VFPOLEDB;" + "Data Source=C:MyPathMyDb.dbc;" + "Collating Sequence=machine;" + "Password=Secret;"; conn.Open();
OleDb — Free table directory
using System.Data.OleDb; var conn = new OleDbConnection(); conn.ConnectionString = "Driver=VFPOLEDB;" + "Data Source=C:MyPath;" + "Collating Sequence=general;" + "Password=Secret;"; conn.Open();
Earn income with your developer skills
Sign up and we’ll send you the best freelance opportunities straight to
your inbox.
We’re building the largest self-service freelancing marketplace for people like you.