我找到了解决方案。 如果在Visual Studio中修改数据库模型Clickonce将在安装后自动重新创建所有表格(具有修改表格的新发布的应用程序)。 ClickOnce的旧数据库保存到指定的位置:
if (ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.DataDirectory != null)
string preDatabase = Path.GetFullPath(Path.Combine(ApplicationDeployment.CurrentDeployment.DataDirectory,@".pre\sampledatabase.mdf"));
这.PRE目录由ClickOnce的创建。你总是检查这个文件是否存在。如果存在,则必须将旧表中的数据复制到新表中或者您将丢失旧表中的所有数据!
如何将数据从一个数据库复制到另一个非常相似的数据库?我的答案如下:使用SqlBulkCopy将所有表复制到另一个表。
// Create source connection
using (
var source =
new SqlConnection(
String.Format(
@"Data Source=(LocalDB)\v11.0;AttachDbFilename={0};Integrated Security=True;",
preDatabase)))
{
source.Open();
// Create destination connection
using (var destination = new SqlConnection(Settings.Default.mdcdbConnectionString))
{
destination.Open();
DataTable dt = source.GetSchema("Tables");
foreach (string tablename in from DataRow row in dt.Rows select (string) row[2])
{
App.Logger.LogText(String.Format("Copying table {0}", tablename));
using (var cmd = new SqlCommand(String.Format("TRUNCATE TABLE {0}", tablename), destination))
{
cmd.ExecuteNonQuery();
//App.Logger.LogText(String.Format("truncate table {0}", tablename));
}
using (var cmd = new SqlCommand(String.Format("SELECT * FROM {0}", tablename), source))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
var bulkData = new SqlBulkCopy(destination)
{
DestinationTableName = tablename
};
// Set destination table name
bulkData.WriteToServer(reader);
// Close objects
bulkData.Close();
//App.Logger.LogText(String.Format("Copy success {0}", tablename));
}
}
}
destination.Close();
}
source.Close();
}
祝你好编码! 戴夫
找到问题 [链接]暗示http://social.msdn.microsoft.com/Forums/windows/en-US/24ada8b9-7a27-4e86-a16a-d6a620d216a0/clickonce-deployment-of -access-database – 2014-08-28 14:19:22
发现另一个提示:http://social.msdn.microsoft.com/forums/windows/en-us/4671a906-0755-438b-80a2-283a0eb5f392/clickonce-deployment-questions – 2014-08-28 14:24:23