Search code examples
c#sqlsql-serverdatabase-connectionlocal

Unable to connect to local database SQL


I have a problem with my local database made with SQL Server (Local DB). I can connect to the database on my computer but if I try to another computer, I get this error message: enter image description here

I want a local database to store data, I don't need a server to manage the database.

This is my connection string:

`<connectionStrings>
    <add name="stocksDB" connectionString="Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\myDB.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>`

I have included the "SQL Server 2012 Express LocalDB" in prerequisites.

enter image description here

What did I do wrong?


Solution

  • Solved! The problem was the wrong SQL Server version on the other computer. On my main computer I have SQL Server 2014 and on the other one the 2012 version so the "database instance name" was different. Thanks to @Nova Sys Eng for the input!

    Now I changed my connection string: First of all I used a code to retrieve all the SQL server instances installed on the computer as explained on the link posted by Nova Sys Eng.

       var instances = GetLocalDBInstances();
       var connString= string.Format("Data Source= (LocalDB)\\{0};AttachDbFilename=|DataDirectory|\\myDB.mdf;Integrated Security=True;",instances[0]);
    
     internal static List<string> GetLocalDBInstances()
            {
                // Start the child process.
                Process p = new Process();
                // Redirect the output stream of the child process.
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.Arguments = "/C sqllocaldb info";
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                p.Start();
                // Do not wait for the child process to exit before
                // reading to the end of its redirected stream.
                // p.WaitForExit();
                // Read the output stream first and then wait.
                string sOutput = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
    
                //If LocalDb is not installed then it will return that 'sqllocaldb' is not recognized as an internal or external command operable program or batch file.
                if (sOutput == null || sOutput.Trim().Length == 0 || sOutput.Contains("not recognized"))
                    return null;
                string[] instances = sOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                List<string> lstInstances = new List<string>();
                foreach (var item in instances)
                {
                    if (item.Trim().Length > 0)
                        lstInstances.Add(item);
                }
                return lstInstances;
            }