Connection string for excel

CData ADO.NET Provider for Excel

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

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

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

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

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

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

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

.NET Framework Data Provider for OLE DB

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;

Microsoft Excel ODBC Driver

Standard

Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=C:MyExcel.xls;DefaultDir=c:mypath;

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;

.NET Framework Data Provider for ODBC

.NET xlReader for Microsoft Excel

Skip to content

Connection String Parameters for Excel Data Sources

Connection String Parameters for Excel Data Sources

Connection String Parameters for Excel Data Sources

In the previous article, I discussed how we can treat Excel and text files as if they were a database using DAO, and how we can open them without linking. Because they do not use ODBC drivers, their connection string will be formatted quite differently from what you might be accustomed to seeing for an ODBC connection string. There’s a dearth of documentation on the Excel connection string parameters. This is a best-effort to cover some of the gaps and discuss the ramifications of parameters.

Excel connection string parameters

Even though we have 3 different data source “types”:

  1. Excel 8.0: 97-2003 xls files
  2. Excel 12.0: xlsb files
  3. Excel 12.0 Xml: xlsx files

They all use the same parameters.

Here are the list of parameters:

HDR parameter: Header row

YES: The first row is the header and should become the column names for the “table”/”recordset”
NO: The first row is not treated differently and is just a data. All column names will be named “FN” where “N” is a number starting with 1

IMEX parameter: Import/Export Behavior

This governs how the column data types should be defined, based on the content:
1: If the column contain different data types, treat it as a string. Otherwise, match the column to the best data type.
2: Always match the column to a certain data type based on the sample. That may cause an error in reading when we read a row that contains data that does not match the expected data type.

ACCDB parameter: Indicates that Access is using ACCDB file format?

By default, this is always set ACCDB=YES in an accdb file format. However, omitting it or setting it to NO seems to do nothing. It’s a bit of a mystery. If anyone can share what effect this parameter, post in comment and I’ll update the blog.

DATABASE: Path to the Excel workbook

The parameter should contain a fully qualified path, including the workbook’s name.

Minimum working connection string

Note that the DATABASE is the only mandatory parameter in addition to the data type source keyword. Therefore a minimum working connection string can be:

Excel 8.0;DATABASE=C:LinksProducts.xls

Specifying sheet or range in connection string

In the previous sample, you saw that a sheet represented a “DAO.TableDef“. However, worksheets aren’t the only thing that can be a “Tabledef“. If the Excel spreadsheet contains a named range, the named range will be reported as a “Tabledef” as well. Additionally, we can “query” an arbitrary block in the sheet using cell address. For example:

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = DBEngine.OpenDatabase(vbNullString, False, False, "Excel 12.0 Xml;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=C:LinksProducts.xlsx")
Set rs = db.OpenRecordsset("Sheet$1A1:A3")

Debug.Print rs.Name, rs.Fields.Count

It’s important to note that the cell addresses cannot exceed the sheet’s used range. For example, the Products.xlsx only actually has contents in A1:B3, that means if you open a recordset using Sheet1$A1:D5, you still get only 2 for fields count and 3 for record count. The extra blank columns/rows are simply ignored. On the flip side, if you dirtied a cell somewhere outside the A1:B3, the sheet’s UsedRange will be now as bigger and querying would then include blank columns and rows.

Therefore those are valid names to use in a query on a Excel “database”:

  1. Sheet1$ – Entire used range of a worksheet.
  2. Sheet1$A1:B4 – Only 2 columns and 3 rows (not counting header), providing that the contents are filled. Otherwise columns or rows may be less than requested.
  3. ProductsRange – the named range with that name.

I find it much nicer to use named ranges where practical as this ensure that you are not hard-coding the addresses in your code especially if the range gets moved around due to user inserting new columns or rows but not altering the contents of the named range. However it is not always practical, especially if you are receiving spreadsheets from a 3rd party and therefore have no control over its contents or formats. In this case, writing a SQL query can work, too.

Querying Excel data source

Suppose we can’t control the format and we don’t want to rely on absolute address even though we are confident that certain columns and rows will be in fact present. In that situation, the best thing to do is to query. Here’s an example that select only one row:

  Dim db As DAO.Database
  Set db = DBEngine.OpenDatabase(vbNullString, False, False, "Excel 12.0 Xml;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=C:LinksProducts.xlsx")

  Dim rs As DAO.Recordset
  Set rs = db.OpenRecordset("SELECT d.[Count] FROM [Sheet1$] AS d WHERE d.[Products] = 'Bananas';")
  Debug.Print rs.Fields(0).Value

Hopefully you can see this is much easier than iterating over each rows to find which one has “Bananas” and then reading the column to right to get the count. In this case, querying beats out automating Excel.

Conclusion

You’ve seen that DAO makes it very easy for us to work with Excel data source and pretend as if it was a relational data source and use our favorite querying language and familiar DAO objects instead of writing bunch of VBA code automating Excel to find the data we want. The connection string parameters are fairly straightforward and as long you have the path, you’re good for linking or opening an Excel spreadsheet.

In the next article we will look at text file connection parameters.

Spread the knowledge!

Comments are closed.

Page load link

Go to Top

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;

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 Man's user avatar

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

arcain's user avatar

arcainarcain

14.8k6 gold badges55 silver badges75 bronze badges

8

Содержание

  1. Connection String Syntax
  2. Connection String Builders
  3. Windows Authentication
  4. SqlClient Connection Strings
  5. Windows authentication with SqlClient
  6. SQL Server authentication with SqlClient
  7. Connect to a named instance of SQL Server
  8. Type System Version Changes
  9. Connecting and Attaching to SQL Server Express User Instances
  10. Using TrustServerCertificate
  11. Enabling Encryption
  12. OleDb Connection Strings
  13. OleDb Connection String Syntax
  14. Using DataDirectory to Connect to Access/Jet
  15. Connecting to Excel
  16. Data Shape Provider Connection String Syntax
  17. Odbc Connection Strings
  18. Using DataDirectory to Connect to Visual FoxPro
  19. Oracle Connection Strings
  20. Excel connection strings
  21. .NET libraries
  22. OLE DB providers
  23. ODBC drivers
  24. Wrappers and others
  25. CData ADO.NET Provider for Excel
  26. Standard
  27. No headers in Excel sheet
  28. Caching data
  29. Caching data and metadata
  30. Cached data only / Offline mode
  31. Using an External Cache Provider
  32. Empty cells always NULL
  33. Empty cells always empty string
  34. Suppress formula calculation errors
  35. Read «tilted sheets», where rows are headers and columns are rows
  36. Do not use formulas, only values
  37. Microsoft ACE OLEDB 12.0
  38. Xlsx files
  39. Treating data as text
  40. Xlsb files
  41. Xlsm files
  42. Excel 97-2003 Xls files with ACE OLEDB 12.0
  43. Microsoft Jet OLE DB 4.0
  44. Standard (Excel)
  45. Standard alternative
  46. .NET Framework Data Provider for OLE DB
  47. 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.

Источник

Понравилась статья? Поделить с друзьями:
  • Connecting word in writing
  • Connecting word in english
  • Connecting word and phrases
  • Connecting to cubes with excel
  • Connecting access database to excel