Sql – How connect to SQL Server Compact 4.0 in ASP.NET

asp.netconnectionsqlsql-server-ce

I want connect to SQL Server Compact 4.0 in my ASP.NET application.

Here is example of code:

protected void Page_Load(object sender, EventArgs e)
{
    string connStr = "Data Source=D:\\MyDB.sdf;";
    string sqlStr = "select * from tblMyTable";

    var sqlDataSrc = new SqlDataSource(connStr, sqlStr);

    GridWithUrls.DataSource = sqlDataSrc;
    GridWithUrls.DataBind();
}

But I have the next error:
"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)"

The SqlDataSource has constructor with three parameters, one of them is 'providerName' so, how to specify that I want definitely use Sql Server Compact provider ?
Also I have added System.Data.SqlServerCe reference..

Best Answer

Try:

providerName = "System.Data.SqlServerCe.4.0"