[英]Embedding SQL Server CE database
I'm working with sql server ce database and C#, and I wondering how to really embed the database file so when I have finish installing the app I don't need to copy-paste the database file to the environment as I set in the following code : 我正在使用sql server ce数据库和C#,并且想知道如何真正嵌入数据库文件,因此当我完成应用程序的安装后,不需要像在设置中那样将数据库文件复制粘贴到环境中。以下代码:
private void sambung()
{
string lokasifile = Environment.CurrentDirectory + "\\compactedition.sdf";
string stringkoneksi = "Data Source = \"" + lokasifile + "\";Password = 'blablabla'; Encrypt = True";
koneksi = new SqlCeConnection(stringkoneksi);
}
Do you have any suggestion to make it possible? 您有什么建议使之成为可能吗? Thanks.
谢谢。
If you copy your database to root directory or upper you can use relative path like this: 如果将数据库复制到根目录或上层目录,则可以使用如下相对路径:
public static string GetLogicBinDebug()
{
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
return Path.Combine(baseDir, @"..\..\database.sdf");
}
Or you can keep connection string in app.config using relative path: 或者,您可以使用相对路径将连接字符串保留在app.config中:
<connectionStrings>
<add name="dbEDMContainer" connectionString="metadata=res://*/dbEDM.csdl|res://*/dbEDM.ssdl|res://*/dbEDM.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\..\..\yourDB.sdf"" providerName="System.Data.EntityClient"/>
Data Source=|DataDirectory|\\ will point to "bin" folder 数据源= | DataDirectory | \\将指向“ bin”文件夹
** If you are asking about how to copy database file to your application folder, you can use this approach: **如果您询问如何将数据库文件复制到应用程序文件夹,则可以使用以下方法:
Include Database file to your project in Visual Studio, open file properties and set Build action to " Content ", Copy to output directory - " copy if newer " or " copy always " 在Visual Studio中将数据库文件包含到您的项目中,打开文件属性,并将“ 构建”操作设置为“ 内容 ”, 复制到输出目录 -“ 如果更新则复制 ”或“ 始终复制 ”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.