1

我正在尝试将 .sdf 文件与我的 winform 连接。这就是我想要做的:

SqlConnection con = new SqlConnection();
        con.ConnectionString=@"Data Source=D:\TestWinform\MyDB.sdf;Persist Security Info=True";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into User(Name) values('" + txt.Text + "')";
        cmd.Connection = con;


        con.Open();   // giving exception in this line
        cmd.ExecuteNonQuery();
        con.Close();

但我面临一个问题,con.Open()它给出了这个例外

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) 

我该怎么办,我猜它找不到该文件,但 .sdf 文件在那个位置所以请帮助我

4

1 回答 1

7

您的连接对象是错误的。您正在使用 Sql compact,因此您必须使用SqlCeConnection. SqlConnection用于连接到 SQL Server 。

按照链接对您的问题进行排序。

这是示例

于 2013-06-04T11:36:12.493 回答