Hi
i want to make backup from my sql, and using save dialog to define save path
this is my code
<pre><pre lang="c#">
string strFileName = string.Empty;
saveFileDialog1.FileName = "backup file ";
saveFileDialog1.Filter = @"SQL Backup Files (*.BAK)|*.BAK |All Files(*.*)|*.*)";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.OverwritePrompt = true;
saveFileDialog1.Title = "Backup SQl files";
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
strFileName = saveFileDialog1.FileName.ToString();
}
try
{
bool bBackUpStatus = true;
Cursor.Current = Cursors.WaitCursor;
if (Directory.Exists(strFileName))
{
if (File.Exists(strFileName))
{
if (MessageBox.Show(@"do you want replace?",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
File.Delete(strFileName );
}
else
bBackUpStatus = false;
}
}
else
Directory.CreateDirectory(strFileName );
if (bBackUpStatus)
{
// Connect to DB
SqlConnection connect;
string con = "data source =" + Dns.GetHostName() + @"\SQLEXPRESS;initial catalog=library;integrated security=true";
connect = new SqlConnection(con);
connect.Open();
//----------------------------------------------------------------------------------------------------
//Execute SQL---------------
SqlCommand command;
command = new SqlCommand("backup database [library] to disk=" + strFileName.ToString() + "", connect);
command.ExecuteNonQuery();
//-------------------------------------------------------------------------------------------------------------------------------
connect.Close();
MessageBox.Show("Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
</pre></pre>