SQL Server connection Error Number:2,State:0,Class:20

user2520212

When trying to connect to a local SQL server (MAMP) I'm getting this exception:

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll System.Data.SqlClient.SqlException (0x80131904): 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) ---> System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at login_page.DatabaseClass.dbRead(String sqlQuery) in C:\Users******\DatabaseClass.cs:line 35 ClientConnectionId:00000000-0000-0000-0000-000000000000 Error Number:2,State:0,Class:20

This is the class I'm using to perform a SELECT SQL function

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace login_page
{
    class DatabaseClass
    {
        public void dbSignIn(String username, String password)
        {
            dbRead("SELECT * FROM user_credentials WHERE username = '" + username + "' AND password = '" + password + "'");
        }

        public void dbRegisterUser()
        {
            dbRead("SQL READ TO DATABASE");
            //dbWrite("SQL WRITE TO DATABASE")
        }

        private void dbRead(String sqlQuery)
        {
            SqlDataReader dataReader;
            SqlCommand command;

            // *** CONNECT TO DATABASE
            Console.WriteLine("** Database Connection: Connecting to database");

            SqlConnection dbConnection = new SqlConnection("User Id=root;" + "Password=root;" + "Server=localhost;" + "Trusted_Connection=true;" + "Database=dbmentum;" + "Connection Timeout=10;");
            try
            {
                dbConnection.Open();
                Console.WriteLine("** Database Connection: Connected to database server");

                // *** READ FROM DATABASE
                command = new SqlCommand(sqlQuery, dbConnection);
                dataReader = command.ExecuteReader();

                while (dataReader.Read())
                {
                    Console.WriteLine(dataReader[0].ToString());    
                    Console.WriteLine(dataReader[1].ToString());
                }

                dataReader.Close();
                command.Dispose();
                dbConnection.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
                MessageBox.Show(e.Message, "Mentum - Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            /*
            // CLOSE DATABASE
            try
            {
                dbConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            } */
        }
    }
}

All appropriate ports are enabled and database details are correct.

derpirscher

You mentioned MAMP which is in my understanding a MySql server. Nonetheless, you are using SqlConnection and SqlCommand which is for connecting to an MS SQL Server. For MySql you need a MySqlConnection, MySqlCommand and so on.

As Prashant Pimpale pointed out the error you are observing is related to network connection. And if it's the case, that you use an MS SQL Server client to connect to a MySQL Database, the reason is obvious. MSSQL default port is 1433, MySQL default port is 3306. So the client will try to connect to port 1433, but there is no service listening. Thus, no connection can be established ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Connection error SQL Server

From Dev

Error establishing connection to SQL Server

From Dev

SQL Server 2008 connection error

From Dev

Error of connection to the MS SQL server

From Dev

SQL Server 2008 R2 connection string error

From Dev

Error Script Connection SQL SERVER 2005

From Dev

SQL Server Express connection string error

From Dev

Method/Property Error on New SQL Server Connection

From Dev

Java Spring jdbc sql server connection error

From Dev

SQL Server Management Studio Connection Error

From Dev

Error on calling a function with connection to SQL server

From Dev

Provider named pipes provider error 40 could not open a connection to SQL Server error 2

From Dev

SQL Server 2014 error: Msg 7738, Level 16, State 2

From Dev

.Net SQL Server connection simple class with procedure

From Dev

SQL Server connection error when checking User Roles inside a view

From Dev

Google Dataproc to SQL Server(based on centos 7) connection error?

From Dev

Google Dataproc to SQL Server(based on centos 7) connection error?

From Dev

Connection to SQL Server Database inside Visual Studio connectionString error

From Dev

Entity Framework: Seemingly random SQL Server connection error

From Dev

I am getting an error 26 connection issue to SQL server

From Dev

Google Cloud SQL: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 0

From Dev

Error connection APNS server

From Dev

Error connection APNS server

From Dev

SQL server connection with spring

From Dev

SQL Server connection pooling

From Dev

pypyodbc connection to sql server

From Dev

PHP connection with SQL server

From Dev

SQL server connection with spring

From Dev

Connection to SQL Server fails

Related Related

  1. 1

    Connection error SQL Server

  2. 2

    Error establishing connection to SQL Server

  3. 3

    SQL Server 2008 connection error

  4. 4

    Error of connection to the MS SQL server

  5. 5

    SQL Server 2008 R2 connection string error

  6. 6

    Error Script Connection SQL SERVER 2005

  7. 7

    SQL Server Express connection string error

  8. 8

    Method/Property Error on New SQL Server Connection

  9. 9

    Java Spring jdbc sql server connection error

  10. 10

    SQL Server Management Studio Connection Error

  11. 11

    Error on calling a function with connection to SQL server

  12. 12

    Provider named pipes provider error 40 could not open a connection to SQL Server error 2

  13. 13

    SQL Server 2014 error: Msg 7738, Level 16, State 2

  14. 14

    .Net SQL Server connection simple class with procedure

  15. 15

    SQL Server connection error when checking User Roles inside a view

  16. 16

    Google Dataproc to SQL Server(based on centos 7) connection error?

  17. 17

    Google Dataproc to SQL Server(based on centos 7) connection error?

  18. 18

    Connection to SQL Server Database inside Visual Studio connectionString error

  19. 19

    Entity Framework: Seemingly random SQL Server connection error

  20. 20

    I am getting an error 26 connection issue to SQL server

  21. 21

    Google Cloud SQL: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 0

  22. 22

    Error connection APNS server

  23. 23

    Error connection APNS server

  24. 24

    SQL server connection with spring

  25. 25

    SQL Server connection pooling

  26. 26

    pypyodbc connection to sql server

  27. 27

    PHP connection with SQL server

  28. 28

    SQL server connection with spring

  29. 29

    Connection to SQL Server fails

HotTag

Archive