-1

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

bagofmilk
  • 1,492
  • 8
  • 34
  • 69
  • 1
    Integrated Security means you are using Windows Authentication to connect to the database, big question is, what windows credential is your program using to run? I usually create SQL logins for programs to use for specific apps and specify user and pwd in the connection string. Also, it would help if you could output the SqlException message to see what's going on – Alex Jul 26 '21 at 19:16
  • @Alex thanks for the input! This is the error message I recieved: `ERROR: 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)` – bagofmilk Jul 26 '21 at 19:26
  • 1
    output Environment.UserName and see the credentials it's using – Alex Jul 26 '21 at 19:30
  • `DefaultAppPool` – bagofmilk Jul 26 '21 at 19:33
  • OK that's an issue, it's using an app pool identity! you can't pass Windows user name and password in the connection string to log in to SQL Server. So I recommend you create a SQL Login for this and pass user and pwd in the connection string. – Alex Jul 26 '21 at 19:34
  • I created a new login with access to only my testdb, but when I try to login i get this error: `A connection was successfully established with the server, but then an error occurred in the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) (Microsoft SQL Server, Error: 223)` – bagofmilk Jul 26 '21 at 19:52
  • Sql Server distinguishes between a `login` and a `user`. You created a login, but now you need that login to map to a user. – Joel Coehoorn Jul 26 '21 at 20:00
  • @JoelCoehoorn I get to the part where it asks me to change my password (as it should). Then it tells me: `Cannot open the default database. Login Failed`. I created the database under `localhost -> Databases -> testdb` – bagofmilk Jul 26 '21 at 20:01
  • Also, you may need to restart the Sql Server service (not just management studio) after turning on sql authentication. – Joel Coehoorn Jul 26 '21 at 20:01
  • good point @JoelCoehoorn , create a login, then a user on the db from the login with db_owner for example. – Alex Jul 26 '21 at 20:04
  • For Integrated Security aka Windows Authentication to work between two different machiness, either the two servers are in the same domain (and therefore have a trust relationship with the same DC) or they are both not in the domain, but the logins are *exactly* the same – Charlieface Jul 26 '21 at 20:05
  • @Charlieface SQL Server and the website are both on the same server – bagofmilk Jul 26 '21 at 20:18
  • Then server name should be just a dot `.` – Charlieface Jul 26 '21 at 22:51

1 Answers1

2

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
Alex
  • 2,247
  • 1
  • 27
  • 37
  • Is the default password 'pwd'? – bagofmilk Jul 26 '21 at 20:10
  • change that to whatever you want as well as [MyLogin] and [MyUser] – Alex Jul 26 '21 at 20:18
  • Check my original post. I created an Edit at the bottom to see where I'm currently at. I'm getting a SQL Server 223 error. Any idea what I can do to resolve this? Google tells me to restart the service, but I've clearly done that. – bagofmilk Jul 26 '21 at 20:30
  • 1
    nevermind. I was using MyUser instead of MyLogin. everything works now. Thanks! – bagofmilk Jul 26 '21 at 20:43