【问题标题】:How to backup database (SQL Server 2008) in C# without using SMO?如何在不使用 SMO 的情况下在 C# 中备份数据库(SQL Server 2008)?
【发布时间】:2011-02-06 13:16:40
【问题描述】:

我有这段代码,但它不工作,但我不知道为什么?

try
{
   saveFileDialog1.Filter = "SQL Server database backup files|*.bak";
   saveFileDialog1.Title = "Database Backup";

   if (saveFileDialog1.ShowDialog() == DialogResult.OK)
   {
      SqlCommand bu2 = new SqlCommand();
      SqlConnection s = new SqlConnection("Data Source=M1-PC;Initial Catalog=master;Integrated Security=True;Pooling=False");

      bu2.CommandText = String.Format("BACKUP DATABASE LA TO DISK='{0}'", saveFileDialog1.FileName);

      s.Open();

      bu2.ExecuteNonQuery();
      s.Close();

      MessageBox.Show("ok");
   }
}
catch (Exception ex)
{
   MessageBox.Show(ex.ToString());
}

我得到了这个错误:

有什么问题?

【问题讨论】:

    标签: c# sql-server database backup


    【解决方案1】:

    您需要将该 SqlConnection 对象分配给 SqlCommand - 试试这个代码:

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string connStr = "Data Source=M1-PC;Initial Catalog=master;Integrated Security=True;Pooling=False";
    
        using(SqlConnection conn = new SqlConnection(connStr))
        {
           string sqlStmt = String.Format("BACKUP DATABASE LA TO DISK='{0}'", saveFileDialog1.FileName);
    
           using(SqlCommand bu2 = new SqlCommand(sqlStmt, conn))
           {
               conn.Open();
               bu2.ExecuteNonQuery();
               conn.Close();
    
               MessageBox.Show("ok");
           }
        }
    }
    

    【讨论】:

    • 当我使用你的代码时,我收到了这个错误消息,i44.tinypic.com/5mbhxx.png - 问题是什么?
    • 好吧,你的 SQL Server 真的有目录 C:\Users\Saleh\Documents 吗?请记住:备份将在SQL Server 机器上完成 - 它不会在您自己的本地 PC 上!
    【解决方案2】:

    你从来没有告诉你的 SqlCommand 使用哪个连接(顺便说一下,这是错误消息所说的,你读过吗?)。首先设置Connection 属性或使用SqlConnection.CreateCommand 创建命令。

    【讨论】:

      【解决方案3】:

      用 C# 备份 Sql Server 2008 数据库(100% 正确)

      using System.Data.SqlClient;
      try{
          SqlConnection con = new SqlConnection(cs.conn()); 
          string database = con.Database.ToString(); 
          string datasource = con.DataSource.ToString(); 
          string connection = con.ConnectionString.ToString(); 
          string file_name =data_loaction+database_name;
          --- "D:\\Hotel BackUp\\" + database + day + month + year + hour + minute + second + ms + ".bak";
      
          con.Close();
          con.Open();                
      
          string str = "Backup Database  [" + database + "] To Disk =N'" + file_name + "'";// With Format;' + char(13),'') From  Master..Sysdatabases  Where [Name] Not In ('tempdb','master','model','msdb') and databasepropertyex ([Name],'Status') = 'online'";
      
          SqlCommand cmd1 = new SqlCommand(str, con);
          int s1 = cmd1.ExecuteNonQuery();
          con.Close();               
          if (s1 >= -1)
                  MessageBox.Show("Database Backup Sucessfull.", "Hotel Management", MessageBoxButtons.OK, MessageBoxIcon.Information);
          else{
                  MessageBox.Show("Database Backup Not Sucessfull.", "Hotel Management", MessageBoxButtons.OK, MessageBoxIcon.Information);
          }
      }
      

      【讨论】:

      • 这已经有一个被接受的答案,你的似乎没有添加任何东西。
      • 永远不要说一段代码100% 正确!要么是错的,要么你会搞砸它......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多