Modifiera befintlig SQL Data Connection

Ändra befintlig SQL-datanslutning med Aspose.Cells för Python via .NET

Nedan visas ett exempel på användning av Aspose.Cells för Python via .NET för att ändra SQL-datanslutningen i arbetsboken. Du kan ladda ner käll-Excel-filen som används i detta kodexempel och den genererade output-Excel-filen från följande länkar.

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