SQL connection string on Windows Server 2008 - c#

I need to connect my C# desktop app on PC 1 with database that exist on SQL server instance at Windows Server 2008. First I cannot make my connection string works.
My connection string is:
Server=(Server_Ip)192.168.1.115\(InstanceName)SQLExpress8;initial
catalog=My_Database;integrated security=True;MultipleActiveResultSets=True;
I also tried:
Server=(ServerName)DATABASE\(InstanceName)SQLExpress8;initial
catalog=My_Database;integrated security=True;MultipleActiveResultSets=True;
But that doesn't work.
By the way I have replaced server with datasource and it still doesn't work.

I am not sure if you are putting the parenthesis in your connection string but this is an example of a working one for sql server
connectionString="Server=testServer\instanceName; uid=readOnlyUser; pwd=1234567; database=testDatabase" providerName="System.Data.SqlClient"
where
testServer = IP or server name
instanceName = instance name, sometimes SQLExpress for sql server express
testDatabase = database name
uid = user name
pwd = password
you CAN connect your windows app to the database, however the server IP has to be public to the internet, you will have to mess with firewall settings in order to do that to allow the IP to be accessed publicly.

Related

How to Connect SQL server from client to Server PC using Connection string in C#

I am trying to connect SQL Server from Client PC using the below-mentioned string, but it Is showing error as "Login failed for user 'sa'"
Tried string:
public static string connString = #"Data Source=(eg.IP)10.0.255.255,1433; Network Library=DBMSSOCN;Initial Catalog=InventoryProjects;User Id=sa;Password=password";
I have provided IP , userid and passward are correct.
Happening:
DB is not connected from Client PC.
Expectation:
But it should connect as I expected.
Can Anyone Please guiding me to proceed Further, because I am not very much familiar in C# and SQL
Points that spring to mind:
(eg.IP) should be removed from the connection string
Is IP address correct - may not be if it is dynamic?
Is the user id and password correct - use of sa user id may not be allowed?
Check - if you are able to run Microsoft SQL Server Management Studio to connect to the SQL database and check the connection string.

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
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
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
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.
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.

How to address a remote MySQL server in the connection string?

I use MySQL .NET Connector to communicate with the mysql server. And everything is fine if I set the connection string as:
<add key="ConnectionString" value="Server = localhost; Port = 3306; Database = test; Uid = root; Pwd = root;" />
I'm going to use my program to reach the MySQL Server remotely from another location. How will the connection string supposed to be set in this case?
I tried:
... Server = http://localhost; Port = ...
just as experimenting, but didn't work. Thank you.
Server = localhost specifies the name of the server connected to. In this case it refers to localhost a special name which always refers to the local computer regardless what it is actually called.
You can change this value to the NETBIOS name, DNS name or IP address of the server.

Difficulty Connect to SQL Server Express on LAN

I'm having trouble connecting to a SQL Server on a machine in our LAN.
i have done the following :
1.) Made the IP of the machine running the Server 10.0.0.7 and made the IP of the other machines on the LAN IP 10.0.0.X . There is no internet on the LAN(and it will stay that way), changeing IP is permitted.
2.)Enabled remote connection by doing This
3.)Used the appropriate connection string.
Now my only Question that remains is regards to the connection string of those who wish to connect on the server, here is the current Con-string:
string sConnection = #"Server=10.0.0.7\MARNUS-PC\MARNUS_HOME; User ID=MARNUS-PC\MARNUS_HOME; Password=somepassword; Initial Catalog=TestDB;";
My questions are :
1.)What do i put the Server portion? here is my Server name and instance name acording to
the management studio - Server name : MARNUS-PC\MARNUS_HOME , Instance name : MARNUS_HOME.
2.)What do i use as user ID? I wish to use Windows authentication instead of sql server authentication(i have enabled both on server), so how should the connection string change and how to do i allow a user from the server's side on the management studio(if needed)?
3.)I asume if I use Windows authentication i do not need the password part of the Con-String?
I apologize for all the questions,but i was taught MS Access and is now making the switch to SQL Server by myself.
Your connection string must be something like....
string sConnection = #"Server=MARNUS-PC\InstanceName; User ID=MARNUS-PC\MARNUS_HOME; Password=somepassword; Initial Catalog=TestDB;";
Instance name is the Sql Server Instance name.
If you are not Sure about your instance name execute the following statement in your Sql Server Management Studio
SELECT ##SERVERNAME
It will return the [ServerName\InstanceName] you can pass the whole returned string to your connection string are your Server name Server=ServerName\InstanceName;
Windows Authentication
To use windows authentication you will have to a connection string something like this..
string sConnection = #"Server=MARNUS-PC\InstanceName; Initial Catalog=TestDB; integrated security=SSPI";
The IP address identifies the computer, so you wouldn't use both the IP address and the computer name. You can use the IP address in the server setting:
Server=10.0.0.6\MARNUS_HOME;
or you can use the computer name:
Server=MARNUS-PC\MARNUS_HOME;
You are correct that you shouldn't specify any password when you are using windows authentication, and you shouldn't specify any user name either, but you need to specify that you are using it:
Trusted_Connection=Yes;

Data source for connection string

What connection string should I use if SSMS connects to it using simply machine name, without instance name?
I mean it connects using the following string: PCName
I used to connect using PCName/SQLExpress. I cannot set correct connection string in my app in order to connect app to database on this machine.
How can I check what data source I should use? I've checked in Sql Server configuration that server instance named as SQLEXPRESS.
So I tried data source as:
.\SQLEXPRESS
PCName\SQLEXPRESS
.
I'm trying to connect to a service-based database, located in my app folder. So I'm using the following connection string:
data source=PCName;attachdbfilename=|DataDirectory|\spareparts.mdf;integrated security=true;user instance=true;multipleactiveresultsets=true;App=EntityFramework;
If SSMS connects via PCName then your application should be able to use Data Source=PCName. However it depends on whether your application is on the same machine as SSMS or not. If on a different machine it might not be able to connect for a variety of reasons. We can't speculate what the problem might be if all you do to describe the issue is "It won't connect" - what does that mean? Do you get an error message? If so, what is it? Make sure:
SQL Browser service is started
TCP/IP is enabled
Add Network=DBMSSOCN; to the connection string
You've also tried the IP address in addition to PCName
Firewall isn't blocking the SQL Server port

Categories

Resources