0

シンプルな Windows C# フォーム アプリケーションを作成しています。しかし、ローカル データベース ファイル (.sdf) に接続できません

私は使っています

using System.Data.SqlServerCe;

これは私のapp.configファイルです

<add name="ConnectionString1"
       connectionString="Data Source=D:\Work Place_VS\Dilshan_Hardware\Dilshan_Hardware\Database\AppDB.sdf"
       providerName="Microsoft.SqlServerCe.Client.3.5" />

 <add name="ConnectionString2"
       connectionString="Data Source=|DataDirectory|\Database\AppDB.sdf"
       providerName="Microsoft.SqlServerCe.Client.3.5" />

次のコードを使用してデータベースファイルを接続しようとしています。

try
   {
      using (SqlCeConnection cn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString()))
   {
     SqlCeCommand cmd = new SqlCeCommand("SELECT routeName FROM tbl_route", cn);
     cn.Open();
     SqlCeDataReader reader1 = cmd.ExecuteReader();

    while (reader1.Read())
    {
      MessageBox.Show("Result :" + reader1[0]);
     }
    }

}
catch (Exception ex)
 {
    MessageBox.Show("Error :" + ex.ToString());

}

私が使用するConnectionString1ConnectionString2、connectionStringを作成しようとしている間、常にcatchブロックにジャンプします。

このコードのエラーを見つけるのを手伝ってください。

4

1 に答える 1

0

Persist SecurityInfo値を追加する必要があると思います

次のことを試してください。

<add name="ConnectionString1"
           connectionString="Data Source=D:\Work Place_VS\Dilshan_Hardware\Dilshan_Hardware\Database\AppDB.sdf;Persist Security Info=false;"
           providerName="Microsoft.SqlServerCe.Client.3.5"
于 2015-12-08T19:48:46.397 に答える