Application using SQL Server Database among a LAN - c#

This has been one annoying piece of needle in a haystack.
The C# application is practically complete and I need to test the cooperation of two or more different machines on the same network, to see if they display the same data from the database.
When I run SQL Server + Visual Studio together during development, there's no problem, I use this connection string and everything works OK:
'#"Data Source=PCNAME\SQLEXPRESS;Initial Catalog=POS;";'
I have now published the app and installed it on another machine that happens to be on the same network. Obviously the connection string will not work on that machine because its name is not PCNAME, it's 2NDPC.
But I doubt the answer is to open the solution, edit the cnx string and re-publish for each machine. Even then, how will they be using the same database?
TL;DR
**What I need **
I want to use the machine I developed the application on initially to be hosting the database, to which the local machines can connect.
I've tried forwarding 1434 port and making rules, I've tried starting tcp/ip process in Server Configuration and making sure SQL Browser Agent Service or what not is running.
But no, I either get and error#25 or nothing happens.
Please help and tell me in explicit steps how I can achieve this goal.

1) Enable TCP/IP in the SQL Server Configuration Manager
2) Make sure SQL Server Browser is running, if not, start it, if you can't, right click > properties > service tab > Start Mode = "Automatic"
This is what (excluding people recommending firewall solutions, different programs/sites and downloading stuff to achieve this) I was missing, that nobody managed to tell me.
So .. depending on what you've been doing recently, you may or may not have changed some default values here and there. I'm not too sure if this is default, or if it happened with my win10 upgrade, but either way this worked.
3) In SQL Server Configuration Manager, right click on your TCP/IP item, hit properties, swtich to the ip addresses tab and put in 1433 in the tcp port field under IP1 and IPALL

Instead of pcname, try using TCP/IP address. PCNAME is Netbios dependant. You also need to make firewall adjustments, make sure SQL server is allowed for remote connections and listening on correct port (although 1433 is the default port, you are not guaranteed to have the correct instance on that port).
I have created and published a four part video series on this subject. Although it is about installing our application, 3 out of 4 parts deal with installing SQL server on the host, configuring it for remote access and accessing from clients. Check if you wish, here is the link to the 1st one:
AccuSQL Installation part 1

You need to load your connection string from a configuration file and on publish run a first run wizard or something similar to set the connection string the first time.
To get your 2NDPC to connect, all you'd need to use is the DNS name of the PC and it should work. You may also need to set SQL Server to allow connections on TCP/IP in SQL Configuration Manager.
Also FYI seeing your comment about using IP addresses, you're doing it right, but you don't use \\ in front of the IP. Just replace your PC name with the IP address. EG:
Data Source=192.168.0.1;Initial Catalog=MyDB; ...

Related

Can I port my C# (SQL Server) app to a different PC?

I have created a project for my college using C# and with SQL Server 2016 and SQL Server 2017 Management Studio.
It is possible that my teacher would want to test it on a different PC, so how could I run it on different PC?
The database I used was made on a local server in my laptop.
Is it possible to port it to another PC?
Sorry if port is a bad word for this but its the best thing I can come up with.
Click here for image
Port is the wrong word, but I do think I understand what you want to do - "move" the application so he can run it on another endpoint.
Generally speaking:
- The other endpoint will need the same version of .Net you're dependent on
- He will need the same version of SQL Server running on his laptop
Step 1: Create a backup of your database
Instructions are here:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/create-a-full-database-backup-sql-server
Step 2: Create an archive (zip/rar, whatever) of Executable
If you haven't compiled it into a standalone EXE and have library dependencies, gather them all (normally in your build directory in your project folder)
Step 3: Move the .bak file and archive to the other computer
Step 4: Restore the database
Instructions are here:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/file-restores-simple-recovery-model
Notes:
As Osman Rahimi noted in the comment, you will need to make sure your connection string in your app isn't hardcoded to your IP or machine name. It is best to use localhost or 127.0.0.1 instead.
An alternative you might want to consider is putting the database IP into a variable you load in the .config file. That way you can concatenate it into your connection string and just instruct your teacher to make the change in the config file.
It could also mean you can leave the database on your workstations if it is on the same network and you have nothing blocking the required SQL Server ports between both endpoints.
The salient code/points of reference for the second point above are as follows:
//import statement to reference library
using System.Configuration;
//reference variable in your code (for your connection-string):
ConfigurationManager.AppSettings["SURVEYPATH_SERVER"].ToString()
//where to add variable in config file
<configuration>
<appSettings>
<add key="SQL_SERVER_IP" value="127.0.0.1" />
</appSettings>
</configuration>
To access to database on another machine
Step 1: both machines should be connected to same local network
Step 2: To share database on different machine please refer the following link
https://learn.microsoft.com/en-us/sql/relational-databases/lesson-2-connecting-from-another-computer
I hope this will help you
Simplest you can access it using IP address of your machine to browse it from different machine. make sure your firewall is open or at least the port 80 is open to be able to browse it from outside.
However if you want to copy all code to a different machine and run it from there you have the following options:
1) Do it manually by copying your code and do you configuration there which might take time and might even error prone especially if you miss some configuration. if you just want to copy code only and leave database in your machine, make sure your firewall is open (at least port 1433)
2) Another way if you are familiar with containerization, you can build and share docker's images and then run containers in the other machine easily.
Some articles:
Overview of .NET and Containers
Introduction to SQL Server Containers
Dockerizing Existing .NET MVC
3) Also you can use vagrant as a different solution to share your box and to download it there or through manual virtualization by developing your application from start (Virtualbox, VMware ..etc)

i want my application to be access by more than one user at the same time SQL database

I have developed an desktop application in C# and sql server 2008.
Now I want my application to be accessed by more than one user at the same time.
The idea is a database should be on 1 PC (server) and application will be installed on other PCs (clients) all connected in a LAN. I want all user from different PCs (clients) can access the DB (on SERVER) and perform CRUD operations.
My conn, cnf file has connection string:
server=.\SQLEXPRESS;Initial Catalog=Database1med;Trusted_Connection=Yes;\
I dont know steps to do this. please help to achieve the idea in implementation.
You need to change you authentication mode to be SQL Server Authentication (Read More Here)
Then you need to change your connection string to be like this:
server=192.168.1.1\SQLEXPRESS;Initial Catalog=Database1med;User Id=myUsername;
Password=myPassword;
Assuming 192.168.1.1 is the IP address of the Server you mentioned.
For that purpose, you mean need to either activate the sa user (Not a good idea for let you users access using sa) or create a new login (Read More here)
For Network access configuration: Read this.
You need to either disable firewall (for testing purposes only - not recommended). or Create Rules using Control Panel => Firewall => Allow a program through Windows Firewall:
Create an inbound rule for port TCP 1433 - allow the connection
Create an outbound rule for port TCP 1433 - allow the connection
Don't forget to restart the SQL-Server instance from the configuration manager after doing all of this.
http://www.linglom.com/it-support/enable-remote-connection-on-sql-server-2008-express/ this is the answer you can check this out

Connecting to SQL Azure database from client applications

I have a bunch of small desktop applications for which I have a simple database for keeping user data (who uses which app and in which version) etc.
I want the apps to connect to Azure SQL server and update database record when they're started. My apps have the ADO.NET connection string hardcoded in them.
It works fine from my home network and my company guest network - however, the corporate network has got some of the ports disabled, and that apparently includes port 1433. As per Microsoft troubleshooting guide, I tried telnet and failed.
C:\Users\xxx>telnet 65.55.74.144 1433
Connecting To 65.55.74.144...Could not open connection to the host, on port 143
: Connect failed
I cannot connect neither via my applications, nor by SQL Server explorer in Visual Studio.
So, the question is - how can I get around this problem? It is highly doubtful that corporate IT will unlock a port just because I ask, besides I want to keep it as simple, low profile and independent as possible. Or maybe my approach is incorrect from the very beginning and I should do stuff differently?
Cheers
Bartek
You can't.
Make your desktop applications talk to web services instead, over HTTP/HTTPS. Among other things this will also allow a more controlled access (right now anyone can connect to your database and modify the data, since your access credentials are publicly shared with your app).
A side effect of using we services is that 80/443 are almost always opened in all corp firewalls.

Connection string for SQL Server Express on remote computer?

I have a WinForms program I am creating for a friend of mine that uses a SQL Server Express database. Locally, I can connect to my SQL Server Express fine and when I deploy the app to his computer, it works also. I'm having difficulty connecting to his SQL Server Express instance from my machine though (I'm trying to run the program in debug mode in vs2012 but connected to his database). The program uses Entity Framework in case that matters (I don't think it does).
We've setup his firewall to allow my IP address to access his computer and his SQL Server... so I can log in via remote desktop and I can also connect using SSMS from my pc and see all the databases.... but why can't I connect using vs2012? I'm thinking it has something to do with the connection string but haven't found a working solution yet.
Here's what I have tried:
Got these from ConnectionStrings.com:
Server=100.100.100.100\SQLExpress;Database=TestDB;User Id=UserID;Password=myPassword;
DataSource=100.100.100.100\SQLExpress;Database=TestDB;User Id=UserID;Password=myPassword;
Obviously the IP address has changed for the purposes of this post.
Any ideas?
You've used the connection string attribute:
DataSource
There is no such thing, and I suspect it was just a typo (it pays to use cut and paste instead of transcribing). There is actually a space in that attribute, so it should be:
Data Source
Here is a list of things you need to check on the other computer:
Is TCP/IP protocol enabled? Go to SQL Server Configuration Manager -> SQL Server Network Configuration -> Protocols for {instance}
What IP addresses are enabled for listening in configuration manager? Go to TCP/IP properties -> IP Addresses tab
SQL Server browser started
Firewall set properly – you want to enable TCP and UDP traffic on port 1433
Server allows remote connections? In SSMS open properties for that instance and check Connections tab.

Connecting to SQL database from other computers

I am pretty new to SQL and I'm stuck on something which is probably a few clicks away.
The program I am building will store the data in the database created on the management studio. Everything works fine on a test application. Now the question is: how should I connect to the database if I want to open the program from another computer? I tried copying the test project on a friend's computer but it cannot find the database as I suppose is obvious because the db is stored on my pc.
i know hundreds of questions like this exist around google but I'm sick of looking at forums reading complicated stuff. any help will be really appreciated.
thanks.
In the database connection settings or script you propably have database server set to 'localhost'
Try setting this to the computer network IP if the other computer is inside the same network.
To connect to it from outside the network (over the internet) You need set the database server setting to you external IP and you have to port-forward (NAT) the SQL server port to your computer.
If you can tell us what database software you are using for the Database I can tell you what port to forward, for more help with forwarding you should ask on serverfault.com and also provide your router/firwall make and model
It depends (at least partly) on how you're connecting to the database in the first place. Normally, you'll have some sort of connection string that tells what computer to connect to. It'll be set to the local computer by default, but changing it to something else is normally just a matter of editing that machine name into the string.
If you're connecting via ODBC, the program will just specify an ODBC connection, and the ODBC connection will specify the machine to connect to. You can use the "Data Sources (ODBC)" control panel to edit these (depending on the version of Windows you're using, it may be in the "Administrative Tools" folder instead of showing up directly in the control panel).
when you want to connect to a sql server over the network there are a few things you should know/check:
is your connection string correct (not localhost)
does the remote sql server accept tcp/ip connections
does the firewall of the remote compture allow the connection
Is the sqlbrowser running on the remote computer (if not you need to specify the port in your connection string.
To make sure this al works the best thing you can do is try to connect from the new pc to the remote database using sql management studio or if you don't want to install the ssms you can try to create an odbc profile that connects to the remote pc. By doing this you can determine if the problem is with the database itself or with your application.

Categories