1

開発中にC#でWindowsフォームアプリケーションを作成しました。次の接続文字列を使用しましたが、正常に動作します

<connectionStrings>
    <add name="myconnection" connectionString="Data Source=ABC-PC\SQLEXPRESS;Initial Catalog=mydatabase;Integrated Security=True"/>
  </connectionStrings>

しかし今、クライアントマシンにアプリケーションをデプロイする必要があり、接続文字列にデータディレクトリオプションを追加する必要があり、これを次のように行いました

<connectionStrings>
   <add name="myconnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; User Instance=True;AttachDbFilename=|DataDirectory|\mydatabase.mdf;  Initial Catalog=mydatabase; "/>
  </connectionStrings>

文字列を変更すると、スローされてエラーが発生しました

物理ファイル「D:\Other Projects\Employee\Employee\bin\Debug\mydatabase.mdf」を開けません。オペレーティング システム エラー 2:「2(指定されたファイルが見つかりません。)」。ファイル 'D:\Other Projects\Employee\Employee\bin\Debug\mydatabase.mdf' をデータベース 'mydatabase' として添付できません。

接続文字列を次のように呼び出します

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);

SQL Server 2008 を使用しました。

4

1 に答える 1