Mevcut SQL Veri Bağlantısını Değiştirme Aspose.Cells ile
Aspose.Cells, mevcut SQL Veri Bağlantısını değiştirme işlemini destekler. Bu makale, Aspose.Cells’ı kullanarak SQL Veri Bağlantısının farklı özelliklerini değiştirmeyi açıklamaktadır.
Microsoft Excel içinde Veri > Bağlantılar menü komutunu kullanarak Veri Bağlantılarını ekleyebilir veya görüntüleyebilirsiniz.
Benzer şekilde, Aspose.Cells, Workbook.DataConnections koleksiyonunu kullanarak Veri Bağlantılarına erişim sağlar ve bu bağlantıları değiştirmenizi sağlar.
Aspose.Cells ile Mevcut SQL Veri Bağlantısını Değiştirme
Aşağıdaki örnek, Aspose.Cells’ı kullanarak Excel dosyasının SQL Veri Bağlantısını değiştirmeyi göstermektedir. Kod içinde kullanılan kaynak Excel dosyasını ve kod tarafından oluşturulan çıktı Excel dosyasını aşağıdaki linklerden indirebilirsiniz.
// 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"); |