简体   繁体   English

如何在不使用SMO的情况下在C#中备份数据库(SQL Server 2008)?

[英]How to backup database (SQL Server 2008) in C# without using SMO?

I have this code and it is not working but I don't why? 我有这个代码,它不工作,但我不是为什么?

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());
}

and I get this error : 我收到此错误:

替代文字

What is the problem? 问题是什么?

You never tell your SqlCommand which connection to use (this is what the error message says by the way, did you read it?). 你永远不会告诉你的SqlCommand使用哪个连接(这就是错误信息所说的方式,你读过它了吗?)。 Either set the Connection property or use SqlConnection.CreateCommand to create the command in the first place. 设置Connection属性或使用SqlConnection.CreateCommand首先创建命令。

You need to assign that SqlConnection object to the SqlCommand - try this code: 您需要将该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");
       }
    }
}

Backup Sql Server 2008 database in C#(100% correct) 在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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中使用SMO备份SQL Server数据库 - Using SMO in C# to backup SQL Server database 使用SMO备份带有C#Windows Forms应用程序的SQL Server 2005数据库时,备份失败 - Using SMO to backup SQL Server 2005 Database with C# Windows Forms Application, Back up failed 如何使用SMO备份SQL Server 2012数据库 - How do I backup a SQL Server 2012 database using SMO 如何使用 C# 中的 SMO 使用文件 STREAM 备份和恢复数据库 - How To Backup And Restore DataBase With FILE STREAM Using SMO in C# 使用C#和SMO,如何将备份设备添加到SQL Server? - Using C# & SMO, how do I add a backup device to a SQL Server? 使用Smo.Backup将SQL Server数据库备份到字符串 - Using Smo.Backup to backup SQL Server database to string 使用C#将SQL Server 2008数据库备份到sql文件(如.sql) - Backup SQL Server 2008 database to a sql file(like .sql) using c# 如何在 smo 中毫无例外地使用 smo 恢复您的 SQL Server 数据库 - How restore your SQL Server database with smo without exception in smo 如何使用C#和SMO终止与SQL Server数据库的所有连接? - How to kill all connections to a SQL Server database using C# and SMO? 如何使用C#SMO将备份从一台链接服务器还原到另一台链接服务器 - How to restore backup form one linked server to another linked server using C# SMO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM