尝试连接到.sdf数据库时出现异常

Ich*_*ann 5 c# sql-server web-services visual-studio-2010

我有数据库存档C:\Users\Pawel\Documents\DB.sdf.我该如何连接它?

下面的简单代码不起作用并生成异常.

码:

[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;
}
Run Code Online (Sandbox Code Playgroud)

结果:
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)

我错过了什么?我该怎么做才能正确打开连接?

编辑:
System.NotSupportedException: SQL Server Compact is not intended for ASP.NET development.
太棒了:P

Dmi*_*try 8

考虑使用System.Data.SqlServerCe.SqlCeConnection:

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

(如有必要,添加对System.Data.SqlServerCe的引用)

另外,要在ASP.NET中使用它,请添加:

AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
Run Code Online (Sandbox Code Playgroud)

  • 阅读此链接:http://forums.asp.net/p/1565425/3898581.aspx似乎那家伙解决了这个问题 (2认同)