Connection error

5

I am creating a Windows Forms application using C # and am having a problem with the connection to SQL Server CE 4.0.

The application database is in the AppData folder of the logged-in user. To get the path of the AppData folder I use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) which in my case returns "C: /Users/richard.dias/AppData/Roaming/".

Within this folder I have the following structure: Company name / Application name. This results in the path C:/Users/richard.dias/AppData/Roaming/Nome da empresa/Nome do aplicativo/ .

In the application folder the database is inside the App_Data folder.

To make the connection I'm passing the absolute path to the .SDF database file and the password for the connection.

Connection string end result: Data Source=C:/Users/richard.dias/AppData/Roaming/Nome da empresa/Nome do aplicativo/App_Data/bd.sdf;Password=123456;

Code for connection opening:

using (var conexao = new SqlConnection(STR_CON))
using (var comando = new SqlCommand())
{
    comando.CommandText = sql;
    comando.Parameters.AddWithValue("@p1", p1);
    comando.Parameters.AddWithValue("@p2", p2);
    comando.Parameters.AddWithValue("@p3", p3);

    conexao.Open();

    comando.Connection = conexao;
    obteveSucesso = comando.ExecuteNonQuery() == 3;

    conexao.Close();
}

When entering open the connection the following error occurs:

  

Network-specific or instance-specific error when connecting 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)

or in English:

  

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)

    
asked by anonymous 28.10.2014 / 16:51

1 answer

7

You should use SqlCeConnection , SqlCeCommand , and the like instead of SqlConnection . Add a reference to System.Data.SqlServerCe.dll .

[Original]

You must use SqlCeConnection , SqlCeCommand etc instead of SqlConnection . Add a reference to System.Data.SqlServerCe.dll .

    
28.10.2014 / 18:43