2016-01-10 58 views
-1

我的连接字符串是:如何使用此连接字符串备份数据库

@"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Application.StartupPath + @"\DataDirectory\sch.mdf;Integrated Security=True" 

该数据库中的C#Windows如何备份窗体应用程序?

+0

想要通过连接字符串备份数据库吗? –

+0

请尝试下面的[使用ADO.NET备份SQL Server数据库](http://stackoverflow.com/questions/3142171/backup-sql-server-database-with-progress) – tchelidze

+3

当您使用'AttachDbFileName ='方法(其中我个人**不**推荐),那么相关数据库将具有与**完整路径和文件名**相同的逻辑名称,一旦它连接到'LocalDB' - 使用该名称执行一个普通的'BACKUP DATABASE [YourDatabaseNameHere]'C# –

回答

0
 folderBrowserDialog1.ShowDialog(); 

     string path1 = folderBrowserDialog1.SelectedPath; 
     //MessageBox.Show(path1); 
     DateTime dmiladi = DateTime.Now; 
     PersianCalendar p = new PersianCalendar(); 

     string strdate = p.GetYear(dmiladi).ToString() + "/" + p.GetMonth(dmiladi).ToString() + "/" + p.GetDayOfMonth(dmiladi).ToString(); 

     string BackUpLocation = path1; 
     string BackUpFileName = "backup_sch"; 
     string DatabaseName = Application.StartupPath + @"\DataDirectory\sch.mdf"; 
     string ServerName = "(LocalDB)\\v11.0"; 
     DatabaseName = "[" + DatabaseName + "]"; 
     string fileUNQ = p.GetYear(dmiladi).ToString() + "_" + p.GetMonth(dmiladi).ToString() + "_" + p.GetDayOfMonth(dmiladi).ToString() + "_time" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString(); 

     BackUpFileName = BackUpFileName + fileUNQ + ".bak"; 
     string SQLBackUp = @"BACKUP DATABASE " + DatabaseName + " TO DISK = N'" + BackUpLocation + @"\" + BackUpFileName + @"'"; 

     string svr = "Server=" + ServerName + ";Database=master;Integrated Security=True"; 
     SqlConnection cnBk; 
     SqlCommand cmdBkUp; 
     cnBk = new SqlConnection(svr); 

     cmdBkUp = new SqlCommand(SQLBackUp, cnBk); 

     cnBk.Open(); 
     cmdBkUp.ExecuteNonQuery();