使用 Aspose.Cells 修改现有 SQL 数据连接
Contents
[
Hide
]
Aspose.Cells 支持修改现有的 SQL 数据连接。本文将讲解如何使用Aspose.Cells修改SQL Data Connection的不同属性。
您可以通过以下方式在 Microsoft Excel 中添加或查看数据连接数据 > 连接菜单命令。
同样,Aspose.Cells 提供了使用 Workbook.DataConnections 集合访问和修改数据连接的方法。
使用 Aspose.Cells 修改现有 SQL 数据连接
以下示例说明了使用 Aspose.Cells 修改工作簿的 SQL 数据连接。您可以从以下链接下载此代码中使用的源 Excel 文件和代码生成的输出 Excel 文件。
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook object | |
Workbook workbook = new Workbook(dataDir + "DataConnection.xlsx"); | |
// Access first Data Connection | |
ExternalConnection conn = workbook.DataConnections[0]; | |
// Change the Data Connection Name and Odc file | |
conn.Name = "MyConnectionName"; | |
conn.OdcFile = "C:\\Users\\MyDefaulConnection.odc"; | |
// Change the Command Type, Command and Connection String | |
DBConnection dbConn = conn as DBConnection; | |
dbConn.CommandType = OLEDBCommandType.SqlStatement; | |
dbConn.Command = "Select * from AdminTable"; | |
dbConn.ConnectionString = "Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False"; | |
// Save the workbook | |
workbook.Save(dataDir + "output_out.xlsx"); |