How connect to SQL Server Compact 4.0 in ASP.NET?

Yara picture Yara · Feb 2, 2011 · Viewed 12.9k times · Source

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..

Answer

onof picture onof · Feb 2, 2011

Try:

providerName = "System.Data.SqlServerCe.4.0"