Search code examples
c#sqlsql-serverconnection-stringwindows-server

SQL Server Connection String issues


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):

enter image description here

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:

  1. I ran the script to create the new Login and Role
  2. After the command completed successfully, I exited SSMS and then restarted SSMS using the Services (see image 2)
  3. I logged into SSMS using the credentials: 'MyUser' and 'P@ssword01'
  4. I received an error stating the login was successful but Shared Memory Provider error (SQL Server error 223)

enter image description here

enter image description here


Solution

  • here are some tips to help resolve the issue:

    1. Make sure your server name is correct, e.g., no typo on the name.

    2. 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)

    3. 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).

    4. Make sure SQL Browser service is running on the server.

    5. 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