尝试获取备份c# localdb时出错

问题描述 投票:0回答:2

我尝试备份数据库,但出现错误 数据库不存在我不知道是什么问题我尝试了一整天但我找不到任何有用的代码我尝试了很多代码都给出了相同的错误请帮助我

tring master_ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Database=Master;Integrated Security=True;Connect Timeout=30;";
        string database = GetDataDirectory();
        MessageBox.Show(database);
        using (SqlConnection masterdbConn = new SqlConnection())
        {
            masterdbConn.ConnectionString = master_ConnectionString;
            masterdbConn.Open();

            using (SqlCommand multiuser_rollback_dbcomm = new SqlCommand())
            {
                multiuser_rollback_dbcomm.Connection = masterdbConn;
                multiuser_rollback_dbcomm.CommandText = @"ALTER DATABASE @DBNAME SET MULTI_USER WITH ROLLBACK IMMEDIATE";
                multiuser_rollback_dbcomm.Parameters.AddWithValue("@DBNAME",database);
                multiuser_rollback_dbcomm.ExecuteNonQuery();
            }
            masterdbConn.Close();
        }

        //SqlConnection.ClearAllPools();

        //using (SqlConnection backupConn = new SqlConnection())
        //{
        //    backupConn.ConnectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;";
        //    backupConn.Open();

        //    using (SqlCommand backupcomm = new SqlCommand())
        //    {
        //        backupcomm.Connection = backupConn;
        //        backupcomm.CommandText = @"BACKUP DATABASE BbCon TO DISK='c:\yourdbname.bak'";
        //        backupcomm.Parameters.AddWithValue("@DBNAME", database);
        //        backupcomm.ExecuteNonQuery();
        //    }
        //    backupConn.Close();
        //}

数据库名称为 BbCon.mdf 它位于 C:\Users sus\Desktop\New Folder (3) bcon_accout_software bcon_accout_software in\x86\Debug

c# localdb
2个回答
2
投票

请尝试以下代码

public string constring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDataBase.mdf;Integrated Security=True";

try
{ 
 SaveFileDialog sd = new SaveFileDialog();
 sd.Filter = "SQL Server database backup files|*.bak";
 sd.Title = "Create Database Backup";
 sd.FileName = "MyDataBase-" + string.Format("{0:dd-MM-yyyy_hh-mm-ss-tt}", DateTime.Now);

 if (sd.ShowDialog() == DialogResult.OK)
 {
  using (bcon = new SqlConnection(constring))
  {
   string sqlStmt = string.Format("backup database [" + System.Windows.Forms.Application.StartupPath + "\\MyDataBase.mdf] to disk='{0}'", sd.FileName);
   using (SqlCommand bu2 = new SqlCommand(sqlStmt, bcon))
   {
    bcon.Open();
    bu2.ExecuteNonQuery();
    bcon.Close();
    MessageBox.Show("Backup Created Sucessfully");
   }
  }
 }
}
catch (Exception){MessageBox.Show("Backup Not Created");}

尝试按照我的建议更改您的连接字符串,但它仍然不起作用,那么您可以使用上面的代码。这对我来说就像魅力一样。


0
投票

Prueba con esto,C# 2022 con LocalDB (2019 Sql Express):

        try
        {
            SaveFileDialog sd = new SaveFileDialog();
            sd.Filter = "SQL Server database backup files|*.bak";
            sd.Title = "Create Database Backup";
            sd.FileName = "DBNAME-" + string.Format("{0:dd-MM-yyyy_hh-mm-ss-tt}.bak", DateTime.Now);

            //string dbPath = Environment.GetEnvironmentVariable("APPDATA") + @"\DBNAME.mdf"; // string dbPath = Application.StartupPath + @"\DBNAME.mdf";
            //string stringCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + dbPath + ";Integrated Security=True;Initial Catalog=DBNAME";
            string stringCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Environment.GetEnvironmentVariable("APPDATA") + @"\Mybackup" + @"\DBNAME.mdf;Integrated Security=True;Initial Catalog=CashFlow";
            string dbNombre = "DBNAME"; // Solo el nombre de la base de datos

            if (sd.ShowDialog() == DialogResult.OK)
            {
                using (SqlConnection bcon = new SqlConnection(stringCon))
                {
                    //string sqlStmt = string.Format("backup database [" + System.Windows.Forms.Application.StartupPath + "\\DBNAME.mdf] to disk='{0}'", sd.FileName);
                    string backupStmt = string.Format(@"backup database [{0}] to disk='{1}' WITH INIT", dbNombre, sd.FileName);
                    using (SqlCommand bu2 = new SqlCommand(backupStmt, bcon))
                    {
                        bcon.Open();
                        bu2.ExecuteNonQuery();
                        bcon.Close();
                        MessageBox.Show("Copia de seguridad creada con éxito");
                    }
                }
            }
        }
        catch (Exception ex) { MessageBox.Show("Copia de seguridad no creada.\n" + ex.Message); 
        }

NOTA:包括“Initial Catalog=DBNAME”como se indica。出口!!

© www.soinside.com 2019 - 2024. All rights reserved.