Check Consistency of a SQL Server backup in C #

1

How can I check the integrity of the backup file of a SQL Server database in C # ?? I used the following namespace and classes to generate the backup:

  Microsoft.SqlServer.Management.Smo {
  ServerConnection
  Server
  Backup
  BackupDeviceItem }

I would like to know how to check the BACKUP010101.BKP file for example.
I thought of something like RESTORE VERIFYONLY , but I do not know how to do in C #.

    
asked by anonymous 06.01.2017 / 18:33

2 answers

2

I did a test here with this code and it works, just an example.

Change it to fit what you exactly want.

It is using RESTORE VERIFYONLY you want, the command will check the backup, if it is good it returns 'OK', otherwise it will return the Sql Server error message.

 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string connectionString1 = (@"Data Source =localhost; Initial Catalog = XXXXXXXXXXXXXXXXXX; Integrated Security = True");
        SqlConnection cn = new SqlConnection(connectionString1);
        cn.Open();
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = @"begin try RESTORE VERIFYONLY FROM DISK = 'c:\home\bkp.bak' SELECT 'OK'; end try begin catch select ERROR_MESSAGE(); end catch";

        cmd.CommandType = CommandType.Text;
        cmd.Connection = cn;
        var resultado = cmd.ExecuteScalar();
        MessageBox.Show(resultado.ToString());
        cn.Close();
    }
 } 
    
06.01.2017 / 19:45
0
using Microsoft.SqlServer.Management.Common;  
using Microsoft.SqlServer.Management.Smo;  
using System;  

class A {  
   public static void Main() {  
      // Connect to the local, default instance of SQL Server.   
      Server srv = new Server();  

      // Reference the AdventureWorks2012 database.             
      Database db = srv.Databases["AdventureWorks2012"];  

      // Note, to use the StringCollection type the System.Collections.Specialized system namespace must be included in the imports statements.   
      System.Collections.Specialized.StringCollection sc;  

      // Run the CheckTables method and display the results from the returned StringCollection variable.   
      sc = db.CheckTables(RepairType.None);  

      foreach (string c in sc) {  
         Console.WriteLine(c);  
      }  
   }  
}  

link

    
06.01.2017 / 19:15