如何使用C#连接到Winform应用程序中的.sdf文件?

Ari*_*Das 1 c# sql-server-ce winforms

我正在尝试使用我的winform连接.sdf文件.这是我想要做的:

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

但我面临一个问题,就是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) 
Run Code Online (Sandbox Code Playgroud)

我应该怎么做,我想它找不到文件,但.sdf文件就在那个位置那么请帮助我这个

Toa*_* Vo 7

您的连接对象是错误的.你正在使用Sql compact,所以你必须使用SqlCeConnection.将SqlConnection用于连接到SQL Server.

按照链接对问题进行排序.

这是样本.