connect website to database on different server - asp.net

There are 2 servers. Server A and Server B. My website is on Server A and my database is on server B. How can I let my website connect to server B's database in asp.net (C#)?
I think it should be set in web.config file. Assume the ip address on server B is 123.45.678.90 and my domain on server A is www.example.com (Will mssql.example.com be useful?)

SQL Server on Server B must allow Remote Connections and the TCP/IP protocol must be enabled and listing to the IP address of Server B. Then you can connect from Server A using the ip address of domain name.
Configure the protocols through the SQL Server Configuration Manager -> SQL Server Network Configuration -> Protocols for MSSQLSERVER (Assuming you have MSSQLSERVER instance)
Enable remote connections by connecting with SQL Server Management studio to the database and ask the properties of the Instance and go to 'Connections' there is an option ' Allow remote connections to this server'
Don't forget at last to check your firewall for port 1433. The connection string can be put in the web.config e.q.:
<connectionStrings>
<add name="myConnectionString" connectionString="server=ip-address;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
And in the C# Code the connection string can be accessed by using:
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

You'll need to create a connection string that points to that database. Here's an example on how to build one using an IP: http://www.connectionstrings.com/sqlconnection/connect-via-an-ip-address/
Here's instructions on how to place that string into your config file and read that into your code: http://www.connectionstrings.com/store-connection-string-in-webconfig/
These connection strings can then be used to run the SQL you need (for instance, creating an SqlConnection object and running commands with SqlCommand objects) like here: https://stackoverflow.com/a/10351647/70471

Related

Connect to a database instance with many instance on the net

I tried to connect to a SQL Server database on the internet via an Asp.net application that is on my local system.
The database is on a server with IP x.x.x.x and on the server we have 3 instances of SQL Server 2005: instance1/MSSQL2005 and instance2/MSSQL2005 and instance3/MSSQL2005.
I can't connect with my application to the instance1/sqlserver2005 on the database XXX on this instance !
This its my connection string that didn't work :
<add name="VestaWSEntities"
connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="server = x.x.x.x ;Data Source=Instance1\MSSQL2005;Initial Catalog=MyDB ;User ID=sa;Password=123;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
Your instance names don't look correct.
Usually a SQL instance is specified in the format server\instance
given the information in the question, it should be something like
x.x.x.x\instance1
x.x.x.x\instance2
x.x.x.x\instance3
Can you connect to the instances in SSMS? You should use the same value in the data source in your connection string
Try replacing this in your connection string
server = x.x.x.x ;Data Source=Instance1\MSSQL2005;
to this
Data Source=x.x.x.x\instance1;Initial Catalog=MyDB;

Is there anyway to connect to local SQL Server database from hosted website?

I create a website with a SQL Server database and put it on a host ...
I just want to know if there is a way that when I open my website on a special PC, I could connect to the PC's SQL Server database for reading and writing ...
Let me know if there is a special connection string for that !
You need to use server IP address and user id , password for connect to database.
Like
<add name="DefaultConnection" connectionString="Data Source=198.0.0.1;Initial Catalog=Database;user id=user1;password=[system];" providerName="System.Data.SqlClient" />
IP - 198.0.0.1
User id - user1
password - [system]
Note -
When you accessing your database from other computer , windows
authentication will not work.
Set sql server authentication in sql server
Allow remote connections to sql server
Allow Sql server port into firewall
Then try.

sql server 2012 allowing remote connection

I want to connect asp.net application hosted at a web server with sql server 2012 db hosted at another remote location.
I have enabled the TCP/IP on sql server, also set port 6322 for ip1 and ipall.
but if i go to my web server and telnet into the db server, no connection is established.
in ASP.net page just to test the connection i am using the connection string as below
<add name="DBConnectionStringTEST"
connectionString="Data Source=<db ip address>; Initial Catalog=<db name>; Persist Security Info=True;User ID=<username>;Password=<password>;connection timeout=0;Max Pool Size = 100;Pooling = True"
providerName="System.Data.SqlClient" />
my questions,
1) is there something i am missing, like wrong port.
2) do i need to give db ip address(client_net_address) or the actual db server address.
3) do i need to ask the server administrator to allow web server ip address to connect.
4) what is the quickest way to test if the web server can talk to the sql server without having to amend the connection string in asp.net application every time i make changes.
NOTE: i have searched other similar topic but couldnt find any answer which worked for me.
Without an explicit port or instance name specification, the default 1433 port will be used. Specify your custom port in the connections string:
<add name="DBConnectionStringTEST"
connectionString="Data Source=<db server ip address or host name>,6322; Initial Catalog=<db name>; Persist Security Info=True;User ID=<username>;Password=<password>;connection timeout=0;Max Pool Size = 100;Pooling = True"
providerName="System.Data.SqlClient" />
An easy way to test network connectivity is with TELNET:
TELNET <db server ip address or host name> 6322
This will show an empty console window if successful. Press ctrl-C to close.
You can also verify port connectivity with the Powershell command below if you don't have TELNET installed:
6322 | % { echo ((new-object Net.Sockets.TcpClient).Connect("<db server ip address or host name>",$_)) "server listening on TCP port $_" }
1) is there something i am missing, like wrong port.
From your posted connection string it looks like you are using default instance. if it's indeed named instance then provide the same info in your connection string.
2) do i need to give db ip address(client_net_address) or the actual
db server address.
It should be actual DB server IP address/Hostname along with Db server installed instance name.
3) do i need to ask the server administrator to allow web server ip
address to connect.
If you are providing correct IP addr/hostname/port/credential then it should allow you to connect through. (provided your DB admin have already created DB user for you)
4) what is the quickest way to test if the web server can talk to the
sql server without having to amend the connection string in asp.net
application every time i make changes.
By creating a UDL file. Open a notepad and save it as test.udl. Once saved -> double click the file -> provide all info and test the connection. It should look like below
EDIT:
To configure a universal data link (.udl) file
Double click on he file named test.udl
On Provider tab select SQL Server Native client 10.0
On Connection tab
enter BRR=SQL-N1 as Server Name
In Username and password section provide SA and password
Click on Test Connection
See Here for more information on this.

Can't connect to sql server box

I am trying to connect to a SQL Server database that is behind a firewall. The asp.net application which is in the DMZ tries to connect to the database, however I am getting an error (The webapp works fine from my local machine):
When I telnet:
open 0.0.0.0 1433
it says:
Connecting to 0.0.0.0 ...
Connection to host lost
Does this mean that the firewall is blocking the incoming request? Or is this something else? (IT support assured me the port is open)
Connection string:
data source=0.0.0.0; initial catalog=mydb;user id=xxxx;password=xxxx;persist security info=True;packet size=4096;
Create a new UDL file on your ASP server. Open the UDL file, use the interface to successfully connect to the SQL server and save the file. Open the UDL file in a text editor and copy the connection string into your application.
If your sql server is behind a firewall (and it's a good thing that it is), then your web app should not be connecting to it directly. Rather, it should be going through some sort of service layer. That can be implemented in a variety of ways with different technologies. This service should be located physically on a machine that can access the db, and the web server should not be allowed to directly connect to the db machine (or cluster).
So, with that said, let's address your connection string. Try adding 'integrated security=true'. Here's an example:
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=MY_SERVER_NAME_OR_IP;Initial Catalog=MyDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Again, it's important to note that the web app itself should only be calling to this service layer (perhaps WCF, or possibly a COM component), not connecting to the data access layer directly. There are lots of samples on how to design these types of applications. Here's one that hopefully may help. http://www.c-sharpcorner.com/UploadFile/SukeshMarla/3-tier-architecture-using-Asp-Net/. Good luck.

ASP SQL connection refused, necessary ports are open

I'm trying to develop an ASP SQL connection to a MSSQL 2005 server. The SQL server is a sub server behind our main domain server, so a port forward has been setup from the WAN > LAN on port 1433 to allow the traffic to access the second server.
I've done a telnet test on my end to see if the port forward is working correctly and it tells me the connection is refused. When I run the script I get the following result:
Microsoft OLE DB Provider for SQL Server error '80004005'
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or
access denied.
The code is as follows:
<%
Dim cn
Dim rs
Dim SQL
SQL = "SELECT count(*) FROM master"
'create an instance of the ADO cn and rs objects
Set cn = Server.CreateObject("adodb.connection")
Set rs = Server.CreateObject("adodb.recordset")
'open the cn to the database
cn.Open "Provider=SQLOLEDB;Data Source=###.###.###.###,1433;Network Library=DBMSSOCN;Initial Catalog=blah;User ID=blah;Password=blah"
'Open the rs object executing the SQL statement and return records
rs.Open SQL,cn,adOpenKeyset,adLockOptimistic
'first of all determine whether there are any records
If rs.EOF Then
Response.Write("No records returned.")
Else
'if there are records then loop through the fields
Do While NOT rs.Eof
Response.write rs("count")
Response.write "<br>"
rs.MoveNext
Loop
End If
'close the cn and rs objects to free up resources
rs.Close
Set rs=nothing
cn.Close
Set cn=nothing
%>
Thanks
Also, check Surface Area Configuration in Start menu under 'Microsoft SQL Server 2005' / 'Configuration Tools' to make sure that TCP is enabled. I think by default only Named Pipes are enabled.
Firewalls
The first thing that can block a connection to SQL Server is a firewall. If you have any firewalls, make sure they are configured to allow connections to SQL Server. The default TCP port that SQL Server uses is 1433. Firewalls include McAfee, Norton, Windows Firewall which ships with Windows XP SP2, and Internet Connection Firewall (ICF) which ships with Windows 2000.
Service verification
Before you can connect to SQL Server 2005, you need to verify that SQL Server is running. By default, the SQL Server Express edition is installed as a named instance (SQLEXPRESS). This means that you need to access it by using (local)\SQLEXPRESS from the local machine. SQLEXPRESS without the prefix will not work. You can also use the 127.0.0.1 IP address on a local machine to avoided DNS related problems.
To verify that the service is running, type sqlcmd –S(local)\SQLEXPRESS at the command prompt. If you see "1>" that means that you managed to connect. Type exit to exit the sqlcmd program.
Connection protocols
SQL Server 2005 supports a number of protocols for connecting client applications with the database server. These protocols are TCP, Named Pipes (NP), Shared Memory (SM), VIA, and HTTP. Only TCP, NP, and SM are supported in SQL Server Express.
By default, only SM is accessible for SQL Server Express on the local machine. This means that connections from a remote machine to SQL Server Express will fail unless TCP and/or NP is enabled. If you want to use TCP and NP, you must explicitly turn them on. Use SQL Server Configuration Manager to explicitly enable TCP and NP for server and client. After enabling TCP and NP, you need to start the SQL Browser service (See SQL Browser below).
If you are connecting remotely, you need to substitute "(local)" with the IP address of the server. You can also use the server name instead of the IP address if DNS can resolve the name.
If you are connecting via a specific IP address, make sure you enable the connection for it. In SQL Configuration Manager, expand the SQL Server 2005 Network Configuration node then select TCP/IP Properties from the pane on the right. Select the IP Addresses tab and change Enabled to Yes for the specific IP address.
SQL Server Configuration Manager
The SQL Server Configuration Manager in SQL Server 2005 and SQL Server Express replaces both Client Network Utility and the Server Network Utility. It allows you to configure the protocols that SQL Server listens to as well as the protocols that ADO.NET 2.0 application can use. However, to configure client protocol for applications that use ADO instead of ADO.NET 2.0, you still need to use the Client Network Utility. The Client Network Utility ships with ADO and is part of Windows 2000, Windows XP, and Windows 2003.
To connect to SQL Server Express remotely, make sure that the server can listen to TCP connections. From the SQL Server Configuration Manager, expand "SQL Server 2005 Network Configuration" and navigate to "Protocols for SQL Server Express" then enable TCP. You need to restart the server for the change to take effect.
If you are using Teratrax Database Manager, you can configure client protocols by clicking on the "Client Network Utility" button in the connection dialog. Make sure that you meet the operating system requirement for Teratrax Database Manager (Windows 2000, Windows XP, or Windows 2003).
SQL Server Browser
SQL Browser is a new service in SQL Server 2005 that is used to identify the ports that named instances listen on. The SM protocol does not use this service. This service is turned off in SQL Server Express by default. This means that the user will have to start this service so that remote access can work. You can start the SQL Browser service from the Service Manager or by typing "NET START SQLBROWSER" from the command line.
SQL Browser listens on UDP port 1434. However, pre-SP3 versions of SQL Server 2000 holding port UDP 1434 may result in failure of SQL Browser name resolution, since they may refuse to give up the port. The workaround is to upgrade all SQL Server 2000/MSDE instances on the machine to SP3 or higher.
This happens when the SQL Server service is not running at the remote machine
You may also want to ensure that the sql server instance allows remote connections
I've done a telnet test on my end to
see if the port forward is working
correctly and it tells me the
connection is refused.
Then one of the following is true:
port forwarding does not work
the SQL host firewall is blocking the port (1433)
there is no process listening on 1433 on target:
SQL server stopped or
SQL not configured to use TCP or
SQL host has multiple-IPs and SQL TCP is not listening on the inbound IP from the forwarder or
SQL server listens on non-default port

Resources