An exception occurred while trying to connect to the .sdf database

I have a database in a file C:\Users\Pawel\Documents\DB.sdf. How can i connect to it?

The simple code below does not work and throws an exception.

the code:

[WebMethod]
public String TestCon()
{
    SqlConnection sql = new System.Data.SqlClient.SqlConnection(
        @"Data Source=C:\Users\Pawel\Documents\DB.sdf");

    string str = "OK";

    try
    {
        sql.Open();
        sql.Close();
    }
    catch (Exception ex)
    {
        str = ex.Message;
    }

    return str;
}

Result:
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)

What am I missing? What should I do to open the connection correctly?

EDIT:
System.NotSupportedException: SQL Server Compact is not intended for ASP.NET development.
 Excellent: P

+5
source share
2 answers

Consider using System.Data.SqlServerCe.SqlCeConnection:

System.Data.SqlServerCe.SqlCeConnection con = 
   new System.Data.SqlServerCe.SqlCeConnection(@"Data Source=C:\Users\Pawel\Documents\DB.sdf");

(add a link to System.Data.SqlServerCe, if necessary)

Also, to use it with ASP.NET, add:

AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
+8
source

-, SQL Compact Edition ( sdf). SqlCeConnection SqlConnection?

: , this.

, System.Data.SqlServerCe.

+1

All Articles