I did a test here with this code and it works, it’s just an example.
Change it to suit exactly what you want.
It is using RESTORE VERIFYONLY that you want, the command will check the backup, if it is good it returns 'OK', if not, it will return the error message of Sql Server.
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();
}
}
It worked, thank you.
– Marcos Dias