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)
Someone uses the same password as me, I have to change the password of the case... D
– Oralista de Sistemas
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.
– Oralista de Sistemas
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.– Richard Dias
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.
– Oralista de Sistemas
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.
– Richard Dias