Connection error

Asked

Viewed 1,710 times

5

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

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

Inside 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 folder App_Data.

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

Final result of connection string: 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();
}

Upon entering open connection the following error occurs:

Instance-specific or network error when connecting to SQL Server. The server was not found or not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (Preview: Named Pipes Provider, error: 40 - Unable to open 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. (Preview: Named Pipes Provider, error: 40 - Could not open a Connection to SQL Server)

  • 1

    Someone uses the same password as me, I have to change the password of the case... D

  • Joking aside, can you open the database with some program like Visual Studio itself, or any of the SQL Management suite? If yes, make sure there is no difference in how these programs mount the connection string. In Visual Studio you can see the string (with the masked password) in the properties tab, when selecting the connection.

  • I didn’t know this @Renan. I looked like you said in VS and the only difference I realized is that the property value Data Source double quotes. I tried to remove spaces from folder names on the way and it didn’t work either.

  • That makes it hard. I recommend two things: make sure you’re using the right driver in your program, and check if you can open the base with Visual Studio.

  • I already checked the driver, and I’ve also opened the base with VS. I don’t know what else to do to try to solve this problem.

1 answer

7


You must use SqlCeConnection, SqlCeCommand and similar 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.

  • Thank you very much. Your answer is right! I changed my code and everything worked fine.

  • 4

    Hi, Erik, Welcome to the Portuguese side of the Stack. I’d Suggest that you do a Google Translate when Answering or Maybe add a Footnote: "Please, Feel free to Translate this Answer". Of Course, we are already free to do that, but it’s a nice Detail.

  • Thanks, I will Remember to do that!

  • 7

    Thank you, I’ll remember to do that! :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.