查询表。 Connection 属性 (Excel)QueryTable.Connection property (Excel)

返回或设置一个字符串, 其中包含以下内容之一:Returns or sets a string that contains one of the following:

  • 允许 Microsoft Excel 连接到 OLE DB 数据源的 OLE DB 设置OLE DB settings that enable Microsoft Excel to connect to an OLE DB data source
  • 允许 Excel 连接到 ODBC 数据源的 ODBC 设置ODBC settings that enable Excel to connect to an ODBC data source
  • 允许 Excel 连接到 web 数据源的 URLA URL that enables Excel to connect to a web data source
  • 文本文件的路径和文件名The path to and file name of a text file
  • 指定数据库或 web 查询的文件的路径和文件名The path to and file name of a file that specifies a database or web query

读/写 VariantRead/write Variant.

语法Syntax

表达式连接expression.Connection

_表达式_一个返回一个查询表**** 对象的表达式。expression An expression that returns a QueryTable object.

说明Remarks

设置connection属性不会立即启动与数据源的连接。Setting the Connection property doesn't immediately initiate the connection to the data source. 您必须使用**Refresh** 方法来建立连接并检索数据。You must use the Refresh method to make the connection and retrieve the data.

有关连接字符串语法的详细信息, 请参阅QueryTables集合的**Add** 方法。For more information about the connection string syntax, see the Add method of the QueryTables collection.

另外,也可以通过选择 Microsoft ActiveX 数据对象 (ADO) 库直接访问数据源。Alternatively, you may choose to access a data source directly by using the Microsoft ActiveX Data Objects (ADO) library instead.

如果使用用户界面导入数据, 则会将来自 web 查询或文本查询的数据作为一个查询表**** 对象导入, 而将所有其他外部数据作为**ListObject** 对象导入。If you import data by using the user interface, data from a web query or a text query is imported as a QueryTable object, while all other external data is imported as a ListObject object.

如果使用对象模型导入数据, 则必须将来自 web 查询或文本查询的数据作为查询表导入****, 而所有其他外部数据均可作为ListObject或查询表导入。 ****If you import data by using the object model, data from a web query or a text query must be imported as a QueryTable, while all other external data can be imported as either a ListObject or a QueryTable.

您可以使用ListObject的查询表属性来访问Connection属性。 You can use the QueryTable property of the ListObject to access the Connection property.

示例Example

此示例为第一张工作表上的第一个查询表提供新的 ODBC 连接信息。This example supplies new ODBC connection information for the first query table on the first worksheet.

Worksheets(1).QueryTables(1) _ 
 .Connection:="ODBC;DSN=96SalesData;UID=Rep21;PWD=NUyHwYQI;"

此示例指定一个文本文件。This example specifies a text file.

Worksheets(1).QueryTables(1) _ 
 Connection := "TEXT;C:\My Documents\19980331.txt"

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.