In Visual Studio .NET, you can easily access databases, their objects, and their properties through connections that are established and saved under the Data Connections
node in Server Explorer. However, these user interface features do not work as expected when you use a Microsoft Excel workbook as a data source.
For example, if you try to configure a data connection to Excel by using the Microsoft Jet 4.0 OLE DB Provider, the connection does not appear in the Data Connections
list, and you never receive an error message. Similarly, if you try to configure an Excel data connection "on the fly" (for example, by using the Data Adapter Configuration Wizard after you drag an OleDbDataAdapter
object from the toolbox to your form), the connection does not appear in the Data Connections
list, and you never receive an error message.
None of the drag-and-drop features that Visual Studio .NET offers for projects with data connections are available with Excel data sources.
There are specific requirements and limitations to using Excel as an ActiveX Data Objects (ADO) data source. These requirements and limitations do not change in Visual Studio .NET because you use the same Jet Provider to access the Excel data.
For additional information about these requirements and limitations, click the article number below
to view the article in the Microsoft Knowledge Base:
HOWTO: Use ADO with Excel Data from Visual Basic or VBA
Although you cannot configure a persistent data connection to Excel in Server Explorer, you can use a Microsoft Access file in place of your Excel file to use the Visual Studio .NET drag-and-drop features for data connections. To do this, follow these steps:
- Start Microsoft Access, create a new Access database with the same name as your Excel file in the same folder as your Excel file.
- On the File menu, point to Get External Data, and then click Import or Link Tables to import or to link the tables from your Excel file. Name the imported or the linked tables according to their names in Excel.
- Start Visual Studio .NET, and then create a new Windows Forms application. Form1 is added to the project by default.
- Add a Button control and a DataGrid control to Form1.
- Follow these steps to add a new data connection:
- In Server Explorer, right-click the Data Connections node, and then click Add Connection.
- On the Provider tab of the Data Link Properties dialog box, click Microsoft Jet 4.0 OLE DB Provider.
- On the Connection tab, select the Access database that you created earlier.
- Test the connection, and then click OK.
- Expand the new Access data connection in Server Explorer, and then expand the list of tables.
- Drag one of the tables from Server Explorer to Form1. Notice that the OleDbConnection and the OleDbDataAdapter objects are added to the component tray.
- Right-click OleDbDataAdapter, and then click Generate Dataset to generate a new typed DataSet.
- Switch to the Code window. If the table that you used represents an Excel worksheet and not a named range, expand the "Windows Form Designer Generated Code" region.
- Follow these steps to modify the code to point to your Excel file:
- Replace all instances of the Excel worksheet name with the syntax that the Jet OLE DB Provider requires. For example, if the imported or the linked table in the Access file is named Sheet1, replace all instances of Sheet1 with [Sheet1$].
- Locate the OleDbConnection1.ConnectionString code. Change the extension of the database file from .mdb to .xls, and then type Excel 8.0 in the Extended Properties argument.
- Delete all of the name/value pairs in the connection string except for the following:
- Data Source
- Extended Properties
- Verify that the modified connection string appears as follows:
Me.OleDbConnection1.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\myfolder\myworkbook.xls;" & _
"Extended Properties=""Excel 8.0"";"
- Add the following code to the Button1_Click event to fill the DataSet:
NOTE: Modify the name of the source table as necessary.
DataGrid1.DataSource = Me.DataSet11.Sheet1
Catch o As System.Data.OleDb.OleDbException
- Run the project. Click the button to fill the data grid.
Without this workaround, you cannot add Excel data sources as persistent data connections in Server Explorer, and you cannot use the Visual Studio .NET wizards to configure Excel data sources for this purpose. Therefore, you must hand code all Microsoft ADO.NET data access that uses Excel files.
Article ID: 316831 - Last Review: March 10, 2006 - Revision: 1.2
- Microsoft Visual Studio .NET 2002 Professional Edition
|kbtshoot kbdatabase kbjet kbprb KB316831|