Difficulty Connect to SQL Server Express on LAN - c#

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;

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.

I want to create a shared database c# windows form application?

This application that i am trying to create, whose "instances/copies" will be installed on multiple PCs of the client and are able to access the same database from another PC of the same client that has SQL server running all the time through the same network(i.e: they have the same router and static IP of server etc).
I had two ways to do this:
To make applications use the same database through synchronization.
To have multiple localdbs that then share the changes a database server.
By following 1st way,
I tried setting up the Connection String like:
SqlConnection con = new SqlConnection("Data Source =[serverPcIP,Port];Integrated Security=True;Connect Timeout=30");
SqlConnection con = new SqlConnection("SERVER=[serverPcIP]; Port=[portno];Integrated Security=True;Connect Timeout=30");
In Server - Security Settings:
I have allowed "SQL Server and Windows Authentication mode"
In Sql Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLEXPRESS => TCP/IP = Enabled; under IP Addresses -> IP1 -> TCP Port = 1433 (port that i used in my connection-string)
In advanced Firewall Setting:
Have enabled the Incoming & Outgoing rules for port 1433.
The exception that i get:
Additional information: 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: TCP Provider, error: 0 - The wait operation timed out.)
Can someone please tell me what configurations (either in connection-String or in SQL server itself) am i missing?
Your connection string lacks the instance name e.g. data source=<hostname or ip>\SQLEXPRESS. If your database listens on the default port 1433 you don't need to pass that with the connection string
If you setup a SQL server, you can choose between the default SQL Server instance or the named instance. For SQL Express the default is to install a named instance with the name SQLEXPRESS. If you don't use the default instance and install a named instance instead, you have to pass it with the connection string.
The default SQL Server instance ist just a named instance with the well known name MSSQLSERVER. So if you installed the database instance as the default instance you can connect with <host>[,port] or <host>\MSSQLSERVER[,port]
based on your screenshot, under IP1, the ip address is active but not enabled.
also, ip address that starts with 169.x.x.x normally would mean you have set up this network card to use DHCP, but the network card failed to have ip address from the DHCP server.
do you mind try to use static ip address?
Consider following steps:
Since you mention the port number using configuration manager, the service required restart. please restart the service if not done so.
Seems you are able to connect via SSMS, once service restarted verify SQL Error log via SSMS -> Managemnt -> SQL Server Logs, you must find a message as follows:
Server is listening on [ 'any' <ipv4> 1433].
Further details, follow these steps. Also, this..

SQL connection string on Windows Server 2008

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.

remote connection to mysql

i have two machine
PC#1 with db mysql and appache and PC#2 with a c# form try to connect to this db
here is my connection string used in PC#2: SERVER=My_public_ip;DATABASE = my_test; UID = root; PASSWORD = xxxx;"
when i try to connect locally from PC#1 it work good:
SERVER=localhost;DATABASE = my_test; UID = root; PASSWORD = xxxx;"
when i try
i did make my root user remote enabled like that:
GRANT ALL ON *.* to '%'#'%' WITH GRANT OPTION;
and all i get is an Event viewer: unable to connect to any of the specified MySQL host
If both MySql installation are similar (no port changes etc.) then you might want to check connection restriction to the port that MySQL is listening to (usually 3306) on the remote machine. A firewall or something of that sort may be preventing access.
Also if I may ask, have you used any other tool(like MySQL WorkBench or similar) to connect to the MySQL on the remote machine, from the machine which is running your c# form.
That may give you an idea as to whether it access restrictions.
Lastly, i suggest you specify the port in the connection string.
"Data Source =SomeName; Database=SomeDb; User Id=SomeUserId; Password=SomePassword; Port=3306;"

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