检索SQL连接数据
Contents
[
Hide
]
Aspose.Cells可以帮助您检索SQL连接数据。这包括连接到SQL服务器所需的所有数据,例如服务器URL,用户名,表名,完整SQL查询,查询类型,表的位置以及与其关联的命名范围的名称。
在Microsoft Excel中,通过以下步骤连接数据库:
- 单击数据菜单,选择来自其他源,然后选择来自SQL Server。
- 然后选择数据,然后选择连接。 3.使用连接向导连接到数据库并创建数据库查询。
在Microsoft Excel中显示SQL连接选项
Aspose.Cells提供了Workbook.getDataConnections()方法来检索外部连接。它返回工作簿中的ExternalConnection对象集合。
如果ExternalConnection对象包含SQL连接数据,则可以将其强制转换为DBConnection对象,使用其属性检索数据库命令,命令类型,连接说明,连接信息,凭证等。
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(RetrieveSQLConnectionData.class); | |
// Create a workbook object from source file | |
Workbook workbook = new Workbook(dataDir + "connection.xlsx"); | |
// Access the external collections | |
ExternalConnectionCollection connections = workbook.getDataConnections(); | |
int connectionCount = connections.getCount(); | |
ExternalConnection connection = null; | |
for (int i = 0; i < connectionCount; i++) { | |
connection = connections.get(i); | |
// Check if the Connection is DBConnection, then retrieve its various properties | |
if (connection instanceof DBConnection) { | |
DBConnection dbConn = (DBConnection) connection; | |
// Retrieve DB Connection Command | |
System.out.println("Command: " + dbConn.getCommand()); | |
// Retrieve DB Connection Command Type | |
System.out.println("Command Type: " + dbConn.getCommandType()); | |
// Retrieve DB Connection Description | |
System.out.println("Description: " + dbConn.getConnectionDescription()); | |
// Retrieve DB Connection ID | |
System.out.println("Id: " + dbConn.getConnectionId()); | |
// Retrieve DB Connection Info | |
System.out.println("Info: " + dbConn.getConnectionInfo()); | |
// Retrieve DB Connection Credentials | |
System.out.println("Credentials: " + dbConn.getCredentials()); | |
// Retrieve DB Connection Name | |
System.out.println("Name: " + dbConn.getName()); | |
// Retrieve DB Connection ODC File | |
System.out.println("OdcFile: " + dbConn.getOdcFile()); | |
// Retrieve DB Connection Source File | |
System.out.println("Source file: " + dbConn.getSourceFile()); | |
// Retrieve DB Connection Type | |
System.out.println("Type: " + dbConn.getType()); | |
// Retrieve DB Connection Parameters Collection | |
ConnectionParameterCollection parameterCollection = dbConn.getParameters(); | |
int paramCount = parameterCollection.getCount(); | |
// Iterate the Parameter Collection | |
for (int j = 0; j < paramCount; j++) { | |
ConnectionParameter param = parameterCollection.get(j); | |
// Retrieve Parameter Cell Reference | |
System.out.println("Cell reference: " + param.getCellReference()); | |
// Retrieve Parameter Name | |
System.out.println("Parameter name: " + param.getName()); | |
// Retrieve Parameter Prompt | |
System.out.println("Prompt: " + param.getPrompt()); | |
// Retrieve Parameter SQL Type | |
System.out.println("SQL Type: " + param.getSqlType()); | |
// Retrieve Parameter Type | |
System.out.println("Param Type: " + param.getType()); | |
// Retrieve Parameter Value | |
System.out.println("Param Value: " + param.getValue()); | |
} // End for | |
} // End if | |
} // End for | |