I am struggling to get a connection established between my windows form c# program and a sqlexpress database which has no username or password is the authentication type is windows authentication.
I believe the problem is with the string but I don't understand, I can connect in a windows console application but not a windows form application. I have tried various connection strings..
The last line produces the error
System.ArgumentException: 'Connection is invalid'
Any positive help is highly appreciated, have looked everywhere and search SO and cannot find similar question
{
// New instance of ExcelEngine created (opening excel with no workbooks open)
using (ExcelEngine excelEngine = new ExcelEngine())
{
// Create excel application object
IApplication application = excelEngine.Excel;
//Assigns default application version
application.DefaultVersion = ExcelVersion.Excel2013;
// New workbook created with one worksheet
IWorkbook workbook = application.Workbooks.Create(1);
//Access a worksheet in workbook
IWorksheet worksheet = workbook.Worksheets[0];
if (worksheet.ListObjects.Count == 0)
{
//Estabilishing the connection in the worksheet
string connectionString = "Server =NBE\\SQLEXPRESS; Initial Catalog = BikeStores; Trusted_Connection = True";
// "Data Source = NICHOLASBOACHIE\\SQLEXPRESS; Initial Catalog = BikeStores; Integrated Security = SSPI";
string query = "SELECT * FROM [BikeStores].[sales].[staffs]";
IConnection connection = workbook.Connections.Add("SQLConnection", "Connection with SQL Server", connectionString, query, ExcelCommandType.Sql);
//Create Excel table from external connection (intitate worksheet)
worksheet.ListObjects.AddEx(ExcelListObjectSourceType.SrcQuery, connection, worksheet.Range["A1"]);
}