SQL接続データの取得
Contents
[
Hide
]
Aspose.Cells for Python via .NET では、SQL 接続データの取得を支援します。これには、SQL サーバーへの接続に必要なすべてのデータ(例:サーバーURL、ユーザー名、テーブル名、完全な SQL クエリー、クエリタイプ、テーブルの位置、および関連付けられた名前付き範囲)が含まれます。
Microsoft Excelでは、データベースに接続するには:
- データメニューをクリックし、その他のソース、その後 SQL Server を選択します。
- 次に、データ、その後 Connections を選択します。
- Connectionsウィザードを使用してデータベースに接続し、データベースクエリを作成します。
Aspose.Cells for Python via .NET は、外部接続を取得するための Workbook.DataConnections プロパティを提供します。これにより、ワークブック内の ExternalConnection オブジェクトのコレクションが返されます。
ExternalConnectionオブジェクトにSQL接続データが含まれている場合、それをDBConnectionオブジェクトに型変換し、データベースコマンド、コマンドの種類、接続の説明、接続情報、資格情報などを取得できます。
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 | |
# 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 a workbook object from source file | |
workbook = Workbook(dataDir + "connection.xlsx") | |
# Access the external collections | |
connections = workbook.data_connections | |
connectionCount = len(connections) | |
connection = None | |
for i in range(connectionCount): | |
connection = connections[i] | |
# Check if the Connection is DBConnection, then retrieve its various properties | |
if connection is DBConnection: | |
dbConn = pycore.cast(DBConnection, connection) | |
# Retrieve DB Connection Command | |
print("Command: " + dbConn.command) | |
# Retrieve DB Connection Command Type | |
print("Command Type: " + str(dbConn.command_type)) | |
# Retrieve DB Connection Description | |
print("Description: " + dbConn.connection_description) | |
# Retrieve DB Connection ID | |
print("Id: " + str(dbConn.connection_id)) | |
# Retrieve DB Connection Info | |
print("Info: " + dbConn.connection_info) | |
# Retrieve DB Connection Credentials | |
print("Credentials: " + str(dbConn.credentials_method_type)) | |
# Retrieve DB Connection Name | |
print("Name: " + dbConn.name) | |
# Retrieve DB Connection ODC File | |
print("OdcFile: " + dbConn.odc_file) | |
# Retrieve DB Connection Source File | |
print("Source file: " + dbConn.source_file) | |
# Retrieve DB Connection Type | |
print("Type: " + str(dbConn.type)) | |
# Retrieve DB Connection Parameters Collection | |
paramCollection = dbConn.parameters | |
paramCount = len(paramCollection) | |
# Iterate the Parameter Collection | |
for j in range(paramCount): | |
param = paramCollection[j] | |
# Retrieve Parameter Cell Reference | |
print("Cell reference: " + param.cell_reference) | |
# Retrieve Parameter Name | |
print("Parameter name: " + param.name) | |
# Retrieve Parameter Prompt | |
print("Prompt: " + param.prompt) | |
# Retrieve Parameter SQL Type | |
print("SQL Type: " + str(param.sql_type)) | |
# Retrieve Parameter Type | |
print("Param Type: " + str(param.type)) | |
# Retrieve Parameter Value | |
print("Param Value: " + str(param.value)) |