次の解決策を探しています。
WPF アプリケーションの参照 (読み取り専用) データを含む、埋め込みリソースとして使用される SQL サーバー コンパクト エディション ファイル (.sdf) への適切な形式の接続文字列を見つけます。
注:出力ディレクトリ設定で「埋め込みリソース」と「コピーしない」に設定された [ビルド アクション]の操作ワードに注意してください。これは、ファイルがスタンドアロン エンティティとしてターゲット コンピューターに物理的にコピーされるのではなく、アプリの実行可能ファイルに埋め込まれていることを意味します。
ここまで、単純なコード スニペットを使用して、埋め込みリソースから .sdf ファイルを取得できるソリューションをテストしました (リスト 1)。
リスト 1。
Assembly _execAssembly = Assembly.GetExecutingAssembly();
// helper snippet to find out all available embedded resource names
string[] _resources = _execAssembly.GetManifestResourceNames();
//.sdf included in IO.Stream
System.IO.Stream _stream
= Assembly.GetExecutingAssembly().GetManifestResourceStream("MyAssemblyName.App_Data.MyDB.sdf");
... _stream
オブジェクトを .sdfに変換しDataSet/TableAdapter
、 またはSystem.Data.SqlServerCe
オブジェクトを使用してこのファイルに接続するには、残りのコードが必要です。SqlCeConnection
、SqlCeCommand
、SqlCeDataReader
次のサンプル コード スニペット (リスト 2) に示すように:
リスト 2。
#region private: Get DataTable using SqlCeDataReader
/// <summary>
/// Get DataTable using SqlCeDataReader
/// </summary>
/// <param name="strConn">string</param>
/// <param name="strSQL">string</param>
/// <returns>DataTable</returns>
private static DataTable GetDataTableFromFileCeReader(string strConn, string strSQL)
{
try
{
using (SqlCeConnection _connSqlCe = new SqlCeConnection(strConn))
{
using (SqlCeCommand _commandSqlCe = new SqlCeCommand())
{
_commandSqlCe.CommandType = CommandType.Text;
_commandSqlCe.Connection = _connSqlCe;
_commandSqlCe.CommandText = strSQL;
_connSqlCe.Open();
using (SqlCeDataReader _drSqlCe = _commandSqlCe.ExecuteReader()) {
DataTable _dt = new DataTable();
_dt.Load(_drSqlCe);
_connSqlCe.Close();
return _dt;
}
}
}
}
catch { throw; }
}
#endregion
ありがとうございます。