Modifier la connexion de données SQL existante

Modifier la connexion SQL existante en utilisant Aspose.Cells pour Python via .NET

L’exemple suivant illustre l’utilisation d’Aspose.Cells pour Python via .NET pour modifier la connexion SQL du classeur. Vous pouvez télécharger le fichier Excel source utilisé dans ce code et le fichier Excel de sortie généré par le code à partir des liens suivants.

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