修改现有SQL数据连接
Contents
[
Hide
]
Aspose.Cells for Python via .NET支持修改现有的SQL数据连接。本文将解释如何使用Aspose.Cells for Python via .NET修改SQL数据连接的不同属性。
你可以通过数据 > 连接菜单命令在Microsoft Excel中添加或查看数据连接。
同样,Aspose.Cells for Python via .NET提供了访问和修改数据连接的手段,使用Workbook.DataConnections集合。
使用Aspose.Cells for Python via .NET修改现有SQL数据连接
以下示例演示了如何使用Aspose.Cells for Python via .NET修改工作簿的SQL数据连接。您可以从以下链接下载此代码用到的源Excel文件和由代码生成的输出Excel文件。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose import pycore | |
from aspose.cells import Workbook | |
from aspose.cells.externalconnections import DBConnection, OLEDBCommandType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create workbook object | |
workbook = Workbook(dataDir + "DataConnection.xlsx") | |
# Access first Data Connection | |
conn = workbook.data_connections[0] | |
# Change the Data Connection Name and Odc file | |
conn.name = "MyConnectionName" | |
conn.odc_file = "C:\\Users\\MyDefaulConnection.odc" | |
# Change the Command Type, Command and Connection String | |
dbConn = pycore.as_of(conn, DBConnection) if pycore.is_assignable(conn, DBConnection) else None | |
dbConn.command_type = OLEDBCommandType.SQL_STATEMENT | |
dbConn.command = "Select * from AdminTable" | |
dbConn.connection_info = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False" | |
# Save the workbook | |
workbook.save(dataDir + "output_out.xlsx") |