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 コンパクトを使用しているため、使用する必要がありますSqlCeConnectionSqlConnectionSQL Server への接続に使用されます。

リンクに従って問題を分類してください。

ここにサンプルがあります。

于 2013-06-04T11:36:12.493 に答える