Search code examples
c#sql-serverdatabasewpfconnection

Connecting to a SQL database in my project folder using connection string in C#


My SQL Server is running, but I'm unable to connect to my database in the project folder. Instead, I'm encountering the following exception:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server."

Here's the code I'm using to connect the database to my project:

public static string ConnectionString()
{
   string path = System.IO.Path.GetFullPath(Environment.CurrentDirectory);    
   string databasename = "riocomsat.mdf";    
   string connectionString = "Data Source=(localdb)\\v11.0;AttachDbFilename=" + path + @"\" + databasename + ";Integrated Security=True";    
   return connectionString;
}

Could anyone help me figure out why this isn't working?


Solution

  • this should work

    Server=(localdb);Database=riocomsat;Trusted_Connection=True;MultipleActiveResultSets=True