I tried to backup local databse (*.mdf), but I have no chance. With dubbging, I got attached deails.
System.Data.SqlClient.SqlException
HResult=0x80131904
Message=An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
Incorrect syntax near 'C:\Users\EngAb\OneDrive\EmguCV\Dataase-2019-12-11--23-11-12.bak'.
Source=.Net SqlClient Data Provider
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at FinalDesignMathSoftware.Form3.button2_Click(Object sender, EventArgs e) in C:\Users\EngAb\source\repos\FinalDesignMathSoftware\FinalDesignMathSoftware\Form3.cs:line 41
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at FinalDesignMathSoftware.Program.Main() in C:\Users\EngAb\source\repos\FinalDesignMathSoftware\FinalDesignMathSoftware\Program.cs:line 19
Any Idea ?
What I have tried:
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Please select path where you want to backup your database", "Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
SqlConnection con = new SqlConnection(conString);
string database = con.Database.ToString();
string cmd = "BACKUP DATABASE [" + database + "] TO Disk '" + textBox1.Text + "\\" + "Dataase" + "-" + DateTime.Now.ToString("yyy-MM-dd--HH-mm-ss") + ".bak'";
using (SqlCommand command = new SqlCommand(cmd, con))
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
command.ExecuteNonQuery();
command.Dispose();
MessageBox.Show("The database is successfully backed up", "Backup process", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}