I have two machines running a C#.net windows forms application each. I want them both to connect to a single SQL Server database which is in one of the machines. And I like both applications to work with that one single database. But Unfortunately I'm getting this UNC share problem. Is there a way to solve this? Or do I have to try a different approach. Please Help Me. I'd appreciate It.
-
2Use SQL Server directly. Using SQL Server over UNC is a very bad idea. – SLaks Mar 24 '13 at 13:48
-
-1. **1.** _"I'm getting this UNC share problem."_ We cannot read your mind. Since this seems to be your main issue, why don't you describe it? **2.** Terrible question title. It's basically a tag list, but doesn't describe your problem at all. – stakx - no longer contributing Mar 24 '13 at 13:49
-
How can i implement that? – Sannan Khan Mar 24 '13 at 13:49
-
1http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx – SLaks Mar 24 '13 at 13:50
-
I'm Sorry for the bad title. =). The problem is that I want my application to manage my general store. But I have two seperate machines networked to carry out the tasks. For that I need a solution on how to implement the connection string so that I could use a single database between two machines. – Sannan Khan Mar 24 '13 at 13:54
-
@CoOlDud3: What did you try? What are you having trouble with? – SLaks Mar 24 '13 at 13:56
-
The database is on machine1-PC's public folder. My other machine2-PC has access to that public folder. i tried this connection string: "Data Source=.\\SQLEXPRESS;AttachDbFilename=\\MACHINE1-PC\\Users\\Public\\Database\\ShopDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True". But I get the error : System.Data.SqlClient.SqlException (0x80131904): An attempt to attach an auto-named database for file \MACHINE1-PC\Users\Public\Database\ShopDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. – Sannan Khan Mar 24 '13 at 14:02
-
2STOP USING ATTACHDBFILENAME. If you want both applications to connect to the same database, attach the database to a single instance of SQL Server, and connect using the logical name of the database, none of this file path nonsense. – Aaron Bertrand Mar 24 '13 at 14:05
-
Okay. 1) How do I attach the database to a single instance of SQL Server? 2) Give me an example of a connection string that i should use for both my machines. Please – Sannan Khan Mar 24 '13 at 14:07
1 Answers
AttachDbFileName
is a terrible and misleading feature. What happens is that each application that connects to this "database" makes a copy of the data file. So if Machine1
connects, and makes changes, those changes are invisible to Machine2
. This causes a lot of confusion even in a single-machine scenario because people will connect via Visual Studio, make some update, and then won't see it from Management Studio. Or vice-versa. The User Instance
feature has been deprecated for a reason; please stop using both of these.
Since you want multiple machines/applications to connect to the same copy of your database, the solution you want is to have a single copy of the database attached to a single instance of SQL Server, and both applications/machines just connect to that single copy.
On MACHINE1-PC
do this:
Move
ShopDatabase.mdf
and its associated.ldf
file out of your user folder and into the data folder for your instance. This will be something like:C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Data\
Connect to the local Express instance using
.\SQLEXPRESS
. Attach the database using the following code:CREATE DATABASE ShopDatabase ON (FILENAME = 'C:\...\ShopDatabase.mdf'), -------------------^^^ fill this in (FILENAME = 'C:\...\ShopDatabase_Log.ldf') -------------------^^^ fill this in FOR ATTACH; -- if there is no log file, you may need to do: CREATE DATABASE ShopDatabase ON (FILENAME = 'C:\...\ShopDatabase.mdf') -------------------^^^ fill this in FOR ATTACH_REBUILD_LOG;
Determine the external IP address of
MACHINE1-PC
(you can do this via ping from another machine - locally it will tell you127.0.0.1
which is useless). Assuming you have a fixed IP and are not using DHCP, this is likely going to be a more reliable way to connect, or at least is missing an extra step (resolving the name). If you use DHCP though you won't want to rely on this because your IP address will potentially change over time. Your connection string should now be:Data Source=MACHINE1-PC\SQLEXPRESS; Network=DBMSSOCN; Integrated Security=True; Initial Catalog=ShopDatabase; -- or: Data Source=<<IP Address>>\SQLEXPRESS; Network=DBMSSOCN; Integrated Security=True; Initial Catalog=ShopDatabase; -- (replace <<IP Address>> of course)
On Machine1
your connection string could use Data Source=.\SQLEXPRESS
, but better to be consistent than to save a couple of keystrokes. This way if you make other changes to your config file etc. they can be distributed to other machines without needing to change the machine name.

- 272,866
- 37
- 466
- 490
-
On MACHINE1-PC. I moved the files. I attached the database using visual studio 2010->New sql express 2008 database project.I used your connection string. After running my app on MACHINE1-PC i get this error: "System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" – Sannan Khan Mar 24 '13 at 15:22
-
@CoOlDud3 Did you try `.\SQLEXPRESS` or `
\SQLEXPRESS`? Can you connect to your database from Management Studio on the same machine? – Aaron Bertrand Mar 24 '13 at 18:18 -
I am using Ray-PC\SQLEXPRESS sir because i am not using a static IP. I currently don't have **Sql Server Management Studio** on either of my machines. I created the database using **Visual Studio 2010**. I tested connection using Visual Studio 2010 which says Connection Succeeded. But then when I run my app it gives me same error as above – Sannan Khan Mar 24 '13 at 20:41