简体   繁体   中英

How do I specify the tables using XlCmdType.xlCmdTableCollection for an Excel Data Model connection in C# / VSTO

Using Connections.Add2 throws an invalid argument exception for the commandText "Table1, Table2,..."

var dataModel = application.ActiveWorkbook.Model;            
var workBookConnection = application.ActiveWorkbook.Connections.Add2("Sample Connection", "Sample Description", connectionString, "Product, ProductCategory", XlCmdType.xlCmdTableCollection, true, true);   
dataModel.AddConnection(workBookConnection);
var tables = new[] { "Product" };
            var cs = "OLEDB;Provider=SQLOLEDB.1;Data Source=localhost;Initial Catalog=AdventureWorks2019;Integrated Security=SSPI;";
            var workBookConnection = application.ActiveWorkbook.Connections.Add2("Sample Connection", "Sample Description", cs, string.Join(",", tables), XlCmdType.xlCmdTableCollection, true, true);   
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM