Remotely connect to SQL Server database - c#

I am using Visual Studio 2013 to develop a Windows form that binds data from a SQL Server database. I want this form to connect to a SQL Server database that is on my server. To try to connect to the server, I do the following:
SQL Server Object Explorer -> Add Server -> Browse for more -> Network Servers
It does not appear in it. Even if I just type the name of the server on the server name I get error 87.
My local machine is using Windows 8, and my server is Windows Server 2012 R2 with SQL Server 2014 Express Edition with Advance Services.
Please, help me with the connection.
Edit:
On the server end I did the following
SQL Server Configuration Manager
-> SQL Server Network Configuration
-> tcp/ip-> enable -> IPALL -> TCP PORT -> 1433
windows firewall advanced settings
-> inbound -> add new rule -> port -> 1433
-> allow connections

Can you connect to the server using Management Studio? If not, check in the SQL Server Configuration Manager you have enabled TCP connection for express edition of SQL server and you have enabled remote connection (TCP port 1433) in windows firewall.

enable the IP that you want to connect
for example i'm using local host
you can use your PC IP :192.168.1.2
or you can use public IP if you have public IP

Related

How to connect with C# app to a SQL server database created on Google Cloud Virtual machine?

I have created a Sql Server Managment Studio database, that is on Google Cloud virtual machine. Now i have C# winform app on a remote pc, and i am trying to retrive a table from that database. Virtual machine is Windows Server 2012R2 Datacenter. SSMS on virtual machine is 2014 and SQL Server 2014.
I get error in C# saying: The server was not found or was not accesible. Error 40- could not open a connection to SQL Server.
My connection string in C# is:
String strConnection=#"Data Source="Virtual machine IP"; Initial Catalog ="db
name"; User
ID="username";Password="password";";
Also i think i could connect to database on virtual machine, using my SSMS on my remote pc. I tried using IP address of virtual machine computer(ipconfig) and used username and password that i created in a VM SSMS. Also tried windows authetication with that ip. But everything with no succes. Error: The server was not found or was not accesible.
-I have allowed remote connections in SSMS.
-Have set SSMS login mode to mixed.
-Have set firewall exceptions on virtual machine for inbound rules, port TCP 1433, port UDP 1434, application sqlservr.exe, application sqlbrowser.exe
-On virtual machine in SQL Server Configuration Manager i have enabled TCP/IP, Named Pipes, Shared Memory
-SQL Server and SQL Browser are running on virtual machine.
If anyone has any ideas what to try next, i would be delightful.
On cloud , you get the URL(connection) from the host page , not just IP address
this work perfect
example for AWS cloud ms sql :
add name='YOURNAMEConnectionString'
connectionString=" Data Source=YOUR_ACCOUNT_LINK.rds.amazonaws.com ;
Initial Catalog=;User ID=;Password=;Connect Timeout=80000 "
providerName="System.Data.SqlClient"

C# Use One SQL Server database on multiple PC

I made an application to insert and view data from service-based database (cDatabase.MDF).
I have 3 PC, I want to run the application in all PC to insert the data in the same database
I'm using SQL Server 2008 Express.
Here are the steps I followed (I'm not sure if they are correct )
open SQL Server Configuration Manager to turn on the TCP/IP from Client Protocol and Protocols for SQL Server Express
Set local Static IP for the PC. 192.168.1.10 , 192.168.1.20 , 192.168.1.30
Set the three PC on one WorkGroup
Turn off the firewall on all PCs
Change connection string to
Data Source=192.168.1.10\SQLEXPRESS;Initial Catalog=cDatabase;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True
I shared my application folder on the network from PC1 ( 192.168.1.10 )
when I open the applicaton from the shared folder (in all PCs) I get this error
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
How to fix this error? And thanks
Set the Integrated Security to False in your Connection String.

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.

Can't connect to localhost on SQL Server Express 2012 / 2016

I just downloaded the latest version of SQL Express 2012 but I cannot connect to localhost. I tried localhost\SQLExpress and Windows authentication but it gives me an error message saying cannot connect. Am I missing something here? I've used SQL Server 2008 before and I've never had issues connecting to localhost. It seems that it can't even find it. Also in the Services I only see a SQL Server VSS Writer. Is this the way it should be? Or am I missing something? Thanks
According to Aaron Bertand:
You need to verify that the SQL Server service is running. You can do this by going to Start > Control Panel > Administrative Tools > Services, and checking that the service SQL Server (SQLEXPRESS) is running. If not, start it.
While you're in the services applet, also make sure that the service SQL Browser is started. If not, start it.
You need to make sure that SQL Server is allowed to use TCP/IP or named pipes. You can turn these on by opening the SQL Server Configuration Manager in Start > Programs > Microsoft SQL Server 2012 > Configuration Tools (or SQL Server Configuration Manager), and make sure that TCP/IP and Named Pipes are enabled. If you don't find the SQL Server Configuration Manager in the Start Menu you can launch the MMC snap-in manually. Check SQL Server Configuration Manager for the path to the snap-in according to your version.
Verify your SQL Server connection authentication mode matches your connection string:
If you're connecting using a username and password, you need to configure SQL Server to accept "SQL Server Authentication Mode":
-- YOU MUST RESTART YOUR SQL SERVER AFTER RUNNING THIS!
USE [master]
GO
DECLARE #SqlServerAndWindowsAuthenticationMode INT = 2;
EXEC xp_instance_regwrite
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'LoginMode',
REG_DWORD,
#SqlServerAndWindowsAuthenticationMode;
GO
If you're connecting using "Integrated Security=true" (Windows Mode), and this error only comes up when debugging in web applications, then you need to add the ApplicationPoolIdentity as a SQL Server login:
otherwise, run Start -> Run -> Services.msc If so, is it running?
If it's not running then
It sounds like you didn't get everything installed. Launch the install file and chose the option "New installation or add features to an existing installation". From there you should be able to make sure the database engine service gets installed.
Goto Start -> Programs -> Microsoft SQL ServerYYYY -> Configuration Tools -> SQL Server YYYY Configuration Manager or run "SQLServerManager12.msc".
Make sure that TCP/IP is enabled under Client Protocols.
Then go into "SQL Server Network Configuration" and double click TCP/IP. Click the "IP Addresses" tab and scroll to the bottom. Under "IP All" remove TCP Dynamic Ports if it is present and set TCP Port to 1433. Click OK and then go back to "SQL Server Services" and restart SQL Server instance. Now you can connect via localhost, at least I could.
Note that this error can of course occur when connecting from other applications as well. Example for a normal C# web application Web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
in SQL SERVER EXPRESS 2012 you should use "(localdb)\MSSQLLocalDB" as Data Source name
for example you can use connection string like this
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;
First try the most popular solution provided by Ravindra Bagale.
If your connection from localhost to the database still fails with error similar to the following:
Can't connect to SQL Server DB. Error: The TCP/IP connection to the host [IP address], port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall."
Open the SQL Server Configuration Manager.
Expand SQL Server Network Configuration for the server instance in question.
Double-click "TCP/IP".
Under the "Protocol" section, set "Enabled" to "Yes".
Under the "IP Addresses" section, set the TCP port under "IP All" (which is 1433 by default).
Under the "IP Addresses" section, find subsections with IP address 127.0.0.1 (for IPv4) and ::1 (for IPv6) and set both "Enabled" and "Active" to "Yes", and TCP port to 1433.
Go to Start > Control Panel > Administrative Tools > Services, and restart the SQL Server service (SQLEXPRESS).
I had a similar problem - maybe my solution will help. I just installed MSSQL EX 2012 (default install) and tried to connect with VS2012 EX. No joy. I then looked at the services, confirmed that SQL Server (SQLEXPRESS) was, indeed running.
However, I saw another interesting service called SQL Server Browser that was disabled. I enabled it, fired it and was then able to retrieve the server name in a new connection in VS2012 EX and connect.
Odd that they would disable a service required for VS to connect.
First check SQL Server Service is Running or stopped, if it is stopped just start it, to do so..just follow the below steps.
1.Start -> Run ->Services.msc
Go to Standard tab in services panel then search for SQl Server(SQL2014)
"SQL2014" is given By me, it may be Another Name in your case
that's it once you start the SQL Service, you are able to connect local database.
hope it will help someone.
All my services were running as expected, and I still couldn't connect.
I had to update the TCP/IP properties section in the SQL Server Configuration Manager for my SQL Server Express protocols, and set the IPALL port to 1433 in order to connect to the server as expected.
After doing the steps which were mentioned by #Ravindra Bagale,
Try this step.
Server name: localhost\{Instance name you were gave}
Try changing from windows authentication to mixed mode
The problem for me was that I was not specifying .\. I was only specifying the name of the instance:
did not work: SQL2016
worked: .\SQL2016
This is odd I have a similar problem. I downloaded the package for SQL 2012 Express with Tools but the Database Engine was not install.
I donloaded the other one from the MS site and this one installed the database engine. After a reboot the services were listed and ready to go.
My situation
empty Instance Name in SQL Server Management Studio > select your database engine > Right Mouse Button > Properties (Server Properties) > Link View connection properties > Product > Instance Name is empty
Data Source=.\SQLEXPRESS did not work => use localhost in web.config (see below)
Solution: in web.config
xxxxxx = name of my database without .mdf
yyyyyy = name of my database in VS2012 database explorer
You can force the use of TCP instead of shared memory, either by prefixing tcp: to the server name in the connection string, or by using localhost.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx
I had the same issue and I found that this happened after I installed an update for my SQL 2012. What fixed it for me was going into programs and features and running a repair on it.
Try changing the User that owns the service to Local System or use your admin account.
Under services, I changed the Service SQL Server (MSSQLSERVER) Log On from NT Service\Sql... To Local System. Right click the service and go to the Log On Tab and select the radio button Local System Account. You could also force another User to run it too if that fits better.

How to connect my APP to SQL Server?

I have 1 PC (Windows Server 2003 + SQL Server 2008)
and 2 PCs (MS SP2 + my APP)
how I can let my APP connects to sql server 2008 on other PC My APP will connect to server to store data in SQL Server 2008 and read/delete/edit (basic operations)
what is the best way to do that?
NOTE: my APP is written in C# (.NET Platform)
Look here:
how to connect to another sql server database(server pc) in local area network
How to connect to SQL server through IP
From
Connection strings for SQL Server 2008 you have:
Connect via an IP address
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;InitialCatalog=myDataBase;User ID=myUsername;Password=myPassword;
DBMSSOCN=TCP/IP. This is how to use TCP/IP instead of Named Pipes. At the end of the Data Source is the port to use. 1433 is the default port for SQL Server.

Categories