I'm on an AWS Windows 2016 server. It comes pre-installed with SQL Server 2016.
I have a .NET 4.8 website where I'm trying to connect the website (on the web server) to the SQL Server instance (also installed on the web server).
I have a simple C# function to tell me if my connection string works (everything I've tried returns false).
public static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}
private static string connect = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
public static string connDb
{
get { return connect; }
set { connect = value; }
}
// Usage:
bool TestConnection = IsServerConnected(connDb);
This is from my web.config
file (connection string)
[I'm only including the **** in the string for privacy reasons, I used the actual SQL Server Name]:
<connectionStrings>
<add name="db"
connectionString="Data Source=EC2AMAZ-84****;Initial Catalog=testdb;Integrated Security=True;Trusted_Connection=Yes;"
providerName="System.Data.SqlClient" />
</connectionStrings>
This is the screen from SQL Server Management Studio (that I'm accessing from the same Windows Server that the website is on):
Can anyone help me out? How should I setup my connection string?
EDIT
From some of the help I received below, this is where I am currently at:
here are some tips to help resolve the issue:
Make sure your server name is correct, e.g., no typo on the name.
Make sure your instance name is correct and there is actually such an instance on your target machine. (Try to use a connection string like .<instance-name> to connect to an instance on your local computer. E.g: .\SQLEXPRESS)
When you try to connect to an SQL Server instance on another server, make sure the server machine is reachable, e.g, DNS can be resolve correctly, you are able to ping the server (not always true).
Make sure SQL Browser service is running on the server.
If firewall is enabled on the server, you need to put sqlbrowser.exe and/or UDP port 1434 into exception.
To create a login and map to db user:
USE [master]
GO
CREATE LOGIN [MyLogin] WITH PASSWORD=N'pwd',
DEFAULT_DATABASE=[master],
DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=OFF,
CHECK_POLICY=OFF
GO
use [testdb];
create user MyUser from login MyLogin;
GO
use [testdb];
exec sp_addrolemember 'db_owner', 'MyUser';
GO