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.
Related
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.
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
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.
I have problems connecting to my database server. The database server is not local, I am connected via its IP address.
It works fine in my development machine. After publishing the website to my server, it can not connect to my database server.
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Why can't my publish server connect to the database server, whereas it works fine through the development machine?
I often had that problem. Mostly it's because of two problems.
Open the SQL Server Configuration Manager.
Check if the SQL Server Network Configuration supports TCP/IP, if it's disabled, enable it.
e.g. SqlServer 2005 Network Configuration, Protocols for SQLEXPRESS
Open the SQL Server Management Studio
Click on the Sql Server Properties (right click on the server name and
select properties).
After that, select the page "Security" and switch the check to "SQL Server and Windows Authentication mode".
That's all.
I'll attempt to go two for two on the psychic debugging for today...
I will assume that you are not using integrated security? If so it might explain things as the account on your local machine probably has permissions, but the SYSTEM account that is running on the server does not. Just a shot in the dark though.
It sounds like your server can't make the network connection, rather than a security issue.
Ensure that any firewalls on both the DB and app servers allow traffic on the port (1433).
Ensure you're able to ping or tracert from both machines.
Is it your first time you publish your website on the web server ? if it is , Are you sure you have set your connection string properly ?
You need to create a login ( of curse a password for that too ) and the IP Address of that SQL on the web server .
So you need IP Address of the sql server host , Database Name , UId , Pwd .
I will agree that this sounds like a network issue and not a security issue.
Remote into the web server and ping the DB server by IP address. If this does not work, your server cannot see the DB server via that address (different subnet, incorrect firewall/proxy setup, etc). There may be a proxy address you must use to get to the DB server from the web server, or your web server may also be the gateway and IIS doesn't know to look for the DB server on the LAN. If it does work, the computers may not be talking on the same port, or the firewall may be blocking that port exiting the web server.
I have spent hours trying to connect to SQL server using sqlcmd. I disabled my firewall, checked all ip listed in "Protocols for SQLEXPRESS", edited my hosts file. I tried using different ips and machinename to connect to the server. But none of work worked. After hours of investigation, I found out that I made absolutely stupid blunder making me unable to connect.
I want to remind people that the connection string is not case sensitive. But the option is!!
what i did is I put
sqlcmd -s .\sqlserver
But the correct string is
sqlcmd -S .\sqlserver
so watch out, people
My company is currently migrating some of their really old db's to sql server 2005. Some legacy apps have problems connecting to the new server. The connection string works in Asp.NET 2.0, probably because it assumes tcp:1433 automatically.
I have to construct the connection string like this in ASP.NET 1.1 for it to work:
"Server=tcp:my.server.com,1433;..."
Without the protocol and the port, the connection fails ("Invalid Connection exception")
TCP 1433 and UDP 1434 are open on our firewall. On SQL Server 2005 Remote Access is enabled, so is TCPIP, the SQL Browser service is running, I use the proper login credentials.
Any ideas why I can't just specify the server name without protocol and port number?
IIRC SQL Server 2005 defaults to find any-old-port that is available. On my laptop this means port 1212.
To force it to a specific port you must go to Start->Programs->SQL Server 2005->Configuration Tools->SQL Server Configuration Manager
From here you must go to SQL Server 2005 Network Configuration->Protocols for (name of service)->Right click on TCP/IP->Properties->Choose tab "IP Address" and set TCP Dynamic Ports to .
For some reason "0" means "Yes, use dynamic ports" and (i.e. no entry in field) menas "No, I will specify it myself"
Then fill in the field TCP Port with 1433.
Do so on all adapters that are listed, and restart the SQL Service.
You can now check if the service is indeed on the right port by doing the following
Start->Run->cmd.exe
C:>netstat -ano
look for an entry like this
local address <stuff> PID
0.0.0.0:1433 <some number>
Now do
C:>tasklist
and look for the task with the number from above. This task should be called something like sqlsrvr.exe.
Perhaps your SQL Server is configured for multi-protocol, and it's trying to use the other protocol first and failing, perhaps for some security reason (the account that the app is running as in IIS). Just a guess.
Check the "SQL Native Client Configuration" settings in "SQL Configuration Manager" on the ASP.NET machine. The default connection settings are set there. Also try messing with the MDAC configuration:
http://msdn.microsoft.com/en-us/library/ms131035.aspx