在第一次发布时写,但之后不要覆盖

本文关键字:之后 覆盖 第一次 布时写 | 更新日期: 2023-09-27 18:14:18

我通过Visual Studio发布了一个使用访问数据库的应用程序,数据库起初是空的,而运行发布的应用程序时,我已经将数据添加到数据库中。

我现在想用我对表单所做的更改来更新应用程序,并添加一个新表,并在原始表中添加列

然而,我想保持新添加的数据是在数据库中,到目前为止,我找不到一种方法来做到这一点,表单要么保持原样,要么数据库被擦除

有人知道我怎么做这个吗?

在第一次发布时写,但之后不要覆盖

下面是一个小片段(未经测试!!)您将不得不调整一些值,但这应该是您的开始。它与Access互操作一起工作,复制db,从新部署的db中删除所有表,从旧的db中导入它们,然后删除临时文件。

var dbPath = "[path to your db]";
var guid = Guid.NewGuid().ToString();
var tempPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), guid);
System.IO.Directory.CreateDirectory(tempPath);
var tempDb = Path.Combine(tempPath, "[yourdbname]");
System.IO.File.Copy("[path to your db]", tempDb, true);
//copy the old database to this tempPath
//Overwrite the original old db with your new one
var access = new Microsoft.Office.Interop.Access.Application();
access.OpenCurrentDatabase(filepath: "[path to your db]", Exclusive: true, bstrPassword: "");
var newdb = access.CurrentDb();
var tableList=new List<string>();
foreach (TableDef table in newdb.TableDefs)
{
    tableList.Add(table.Name);
    newdb.TableDefs.Delete(table.Name);
}
foreach (var table in tableList)
{
    access.DoCmd.TransferDatabase(AcDataTransferType.acImport, DatabaseTypeEnum.dbVersion140, tempDb,
        AcObjectType.acTable, table, table, false, false);
}
System.IO.Directory.Delete(tempPath, true);