如何将数据库文件备份从mdf更改为SQL Server数据库 [英] How do I change the database file backup from an mdf to an SQL server database
问题描述
大家好,
编程新手。我有一个朋友开发的项目正在研究和辅导自己。他有一个mdf文件中的数据库,并且有一个数据库的代码,但是我开发了另一个项目但是使用的sql数据库不像他在.mdf我的作为sql server中数据库的连接字符串,但当我使用他的代码时我得到了一个错误。下面是他的代码,我需要修改什么。
Hello everyone,
Am new to programming. I have project developed by a friend which am working on and tutoring myself on it. He has a database in an mdf file and has a code for the database back but i develop another project but using an sql database not like his in .mdf mine as a connection string to database in sql server but when i used his on code i got an error. below is his code and what do i need to modify.
private void databaseBackupToolStripMenuItem2_Click(object sender, EventArgs e)
{
try
{
Cursor = Cursors.WaitCursor;
timer1.Enabled = true;
if ((!System.IO.Directory.Exists("C:\\DBBackup")))
{
System.IO.Directory.CreateDirectory("C:\\DBBackup");
}
string destdir = "C:\\DBBackup\\ERPS_DB " + DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".bak";
con = new SqlConnection(cs.DBcon);
con.Open();
string cb = "backup database [" + System.Windows.Forms.Application.StartupPath + "\\ERPS_DB.mdf] to disk='" + destdir + "'with init,stats=10";
cmd = new SqlCommand(cb);
cmd.Connection = con;
cmd.ExecuteReader();
con.Close();
st1 = User.Text;
st2 = "Successfully backup the database";
cf.LogFunc(st1, System.DateTime.Now, st2);
MessageBox.Show("Successfully performed", "Database Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我的尝试:
i尝试将数据库名称从他自己的数据库名称更改为我自己的数据库名称,但似乎他在一个文件中,但我自己的数据库是在一个sql服务器中,它不包含在项目文件中他自己的
What I have tried:
i tried changing the database name from his own to my own database name but it seems his is in a file but my own database is in an sql sever which is not included in the project file like his own
推荐答案
请参阅本教程,了解如何在其他服务器上恢复备份:
我如何...恢复SQL服务器数据库到新服务器? - TechRepublic [ ^ ]
在这里查看更多教程: MS SQL Server还原数据库 [ ^ ]
See this tutorial on how to restore backups on a different server:
How Do I... Restore a SQL Server database to a new server? - TechRepublic[^]
And see here for more tutorials: MS SQL Server Restoring Databases[^]
这篇关于如何将数据库文件备份从mdf更改为SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!