Fabrizio402
Fabrizio402

Reputation: 49

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?

Upvotes: -1

Views: 1220

Answers (3)

XB_08
XB_08

Reputation: 1

  1. if you install the app in C: drive Go to the path where you install your aplication then make to the parent directory a Full control because you can't access to the file db and write in it
  2. or install your application in other path where he have a full control

Upvotes: 0

VA systems engineer
VA systems engineer

Reputation: 3189

If you have two computers (lets say their machine names are "pc_a" and "pc_b" that are networked together and the program is running on computer "pc_a" and the database resides on computer "pc_b", then your connect string needs to include the machine name for computer "pc_b".

You can provide the machine name even if it is the local machine, so the code below will work if the program is running on the same machine as the database or if the program is running on one machine and the database is on another, so long as the two machines are networked AND the account you're running the program under has access to the machine and instance and database.

Please note in example below, the "default" instance name (MSSQLSERVER) was used when SQL was installed. When the DB instance name is the default name, then you must not provide an instance name explicitly (you'll get the error you showed if you do). The only time you provide an instance name explicitly is when it it not the default instance name. The code below can handle either scenario (by setting dbInstanceName variable to "" or an instance name, e.g. "\SQLEXPRESS"). See S.O. SQL Server: How to find all localdb instance names. When it doubt, try an empty instance name and a name you believe to be the instance name to see what works.

string databaseMachineName = "pc_b";
string databaseInstanceName = ""; 
string dbName = "stocksDb";

using (SqlConnection sqlConnection = new SqlConnection("Data Source=" + databaseMachineName + databaseInstanceName + "; Initial Catalog=" + dbName + "; Integrated Security=True;Connection Timeout=10"))
{
   .
   .
   .
}

Upvotes: 1

Fabrizio402
Fabrizio402

Reputation: 49

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;
        }

Upvotes: 0

Related Questions