wren
wren

Reputation: 383

How do I let multiple computers share a single database on a server?

I wrote a program to track the records of an employee in Windows Forms application in Visual Studio. I am using SQL Server 2017 Express for the data storage. My SqlConnection statement is using localhost and SQL Server authentication, what I need to know if how do I let multiple computers be in sync to a single database, there is no internet but the computers share a server where they can store files inside and all the computers can access it, I read somewhere that people input the ip address of the main computer in the SqlConnection statement but I'm not sure how that would work.

This is my SqlConnection statement currently

new sqlConnection(@"Data source = localhost\sqlexpress; Initial catalog = database;user id =sa;password = 1234; integrated security = false")

The program will be installed on multiple computers for people to use

Upvotes: 0

Views: 3321

Answers (2)

Aris Skam
Aris Skam

Reputation: 116

First of all you have to change localhost with the host name or with the ip of the sql server hosting pc. it will be something

new sqlConnection(@"Data source = mySqlServerPcName\sqlexpress; Initial catalog = database;user id =sa;password = 1234; integrated security = false")

then you have to enable tcp/ip on the sql server Configuration Manager on the hosting pc.

open sql server Configuration Manager

sql server Configuration Manager

then go to SQL Server Network Configuration and click on Protocols for SQLEXPRESS. Then you will see that TCP/IP is disabled. Double Click on TCP/IP and select yes to enabled. click ok and you will get the following message

Warning message

press ok and click on the left side SQL Server Services. Right click the SQL Server(SQLEXPRESS) and select restart. Now your SQL Server is ready.

Now the last thing you have to do is open the sql port on the firewall. open a command line and run the following command

netsh advfirewall firewall add rule name = SQLPort dir = in protocol = tcp action = allow localport = 1433 remoteip = localsubnet profile = DOMAIN

Now you are ready to connect to the database from other computers.

Upvotes: 1

josejamesp
josejamesp

Reputation: 52

Make sure that the sql server on a machine is accessible from all computers you intent the app to work. You also may have to open ports used by sql server in firewall. You can then update the connection string to connect to the sql instance.

Note: It can be a good idea to store connection string in a configuration file rather than in the code.

Upvotes: 0

Related Questions