Search code examples
c#sqlconnectionlocaldb

SQL database connection failure in another computer through |DataDirectory|


I have a database and a server application and everything works fine while I'm running it on my computer.

string ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename='|DataDirectory|\Fabrica.mdf';Integrated Security=True;Connect Timeout=30";

The problem is: I have to run this application in another computer and the database must be there as well, when the server application tries to access the database it returns nothing. Is it a problem with the authentication? File path?

@Edit: here's how I'm accessing the tables:

SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("SELECT * FROM {0}", Table);
string name;
SqlDataReader reader;
try
{
    con.Open();
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        name = reader["Name"].ToString();
    }
    con.Close();
}
catch (Exception e)
{
    MessageBox.Show(e.ToString());
}

@Edit: I don't know if this helps but I have SQL Server LocalDB and SQL Server Express installed in the other computer.


Solution

  • Both authorization and filepath are important.

    LocalDB is installed in Computer A and LocalDB, SQL Sever Express is installed in Computer B(Server) ?

    LocalDB is what the name 'Local' means. This means if you want to move to another computer, you have to re-localize the LocalDB to Computer B.

    Manual way is complicated that you have to do detach the LocalDB from the LocalDB Server in Computer A(ALTER DATABASE IMMEDIATE ROLLBACK has to be done in advance), and copy the LocalDB file to Computer B and attach the LocalDB file to the LocalDB Server in Computer B. And the attachDBfilepath should indicate right path. |DataDirectory| is data directory of the Server application and it's not the execution directory of the Server application. And importantly, detaching and attaching process is needed because of the ownership of the LocalDB.

    Rather than above complicated manual way, I recommend automatic way to add the LocalDB .mdf file to your project of Server application and make sure the property is 'content' and 'copy if newer' or 'copy always'. Utilize the automatically provided connectionstring but just change the part to |DataDirectory|. If you deploy the Server application then, the LocalDB will be deployed as well to the data directory of Server application and the ownership of LocalDB will be automatically changed to the Computer B.

    I'm not sure how you made the application is working fine in Computer A with |DataDirectory| and want to advise understanding the nature of LocalDB and the meaning of |DataDirectory| and connectionstring.

    As per my difficult experience, and this is my private opinion that if you use |DataDirectory|, you can only read(select) but you will not be able to insert(modify) data. This means we need to set(insert) all the data before we add the LocalDB to project to use as |DataDirectory|.

    As a reference, my case is that I set(inserted) already fixed big data into LocalDB and add it to project before deploy and I coded my application to create a new another LocalDB after deployment for insert(modify) function.

    Also, please refer to my answer to someone else.
    Not inserting data into database and not getting any error