Marko
Marko

Reputation: 417

How to run .mdf database from LAN?

I have a C# (.NET Framework 4.5 - MVS 2015) project which has a Service-based Database with a local generated .mdf file. My Microsoft SQL Server that I use has this version: 13.0.1601.5. Everything works fine on the server PC. I share in LAN the application with the .mdf files, but the clients from the same LAN can't open the application. It seems that the connection with the DB is bad.

My connection string is formed like this:

public static string attachedDbFile = "AttachDbFilename=" + currentWorkinglocation + "NCI_DB.MDF;";

public SqlConnection mySqlConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;" + attachedDbFile + "Integrated Security=True");

What am I missing, please?

Later Edit:

After following the steps provided by you guys:

My Data Source is Microsoft SQL Server now (and not MSQL Server Db file). I have attached the .mdf file to a database from my server (in MSQL Server Management Studio), the "Allow remote connection option" is checked and I have changed the connection string to:

public SqlConnection mySqlConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=myDbName;Integrated Security=True");

Now, the application works on my local host (the PC that has the sql server installed) without the need of the .mdf file. It's ok.

But if the clients try to access the application from LAN, from my shared location, the app .exe crashes once again with this error message:

enter image description here

Did I missed something?

SOLUTION: After following some pointers from our colleagues and after investigating further the problem the solution is: - install sql server 2017 (full version ! - not express edition) - install microsoft sql server management studio - attach mdf to a database - configure an account with rights for that database - update the connection string according to this:

public SqlConnection mySqlConnection = new SqlConnection(@"Data Source=ip,port(1433-default);Initial Catalog=db_name;User ID=user;Password=pwd");

Upvotes: 0

Views: 599

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239654

SQL server only supports database files on local disks (or more exotic links than LANs, such as iSCSI, etc). If you want to share access to a database across a local network, it's time to stand up a SQL Server instance that will own the file and then connect to that SQL Server client/server. Stop trying to access it as a file across the network.

Upvotes: 5

Related Questions