Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:

C#
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);



                }
            }
        }
Posted
Updated 11-Dec-19 14:00pm

1 solution

Hi I checked exception detail.
Incorrect syntax near 'C:\Users\EngAb\OneDrive\EmguCV\Dataase-2019-12-11--23-11-12.bak'.
Source=.Net SqlClient Data Provider
this is a error about sql grammer .

your sql command:
string cmd = "BACKUP DATABASE [" + database + "] 
TO Disk 
'" + textBox1.Text + "\\" + "Dataase" + "-" + DateTime.Now.ToString("yyy-MM-dd--HH-mm-ss") + ".bak'";


right sql command:
string cmd = "BACKUP DATABASE [" + database + "] 
TO Disk =N  /*check me */
'" + textBox1.Text + "\\" + "Dataase" + "-" + DateTime.Now.ToString("yyy-MM-dd--HH-mm-ss") + ".bak'";
 
Share this answer
 
Comments
EngAb1989 12-Dec-19 18:33pm    
Hallo. Thanks for your answer. Unlikely still does not work my code. Definitely your code help to skip the syntax error. The debug shows
ystem.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.
Source=.Net SqlClient Data Provider
StackTrace:
Suo ZhengWei 16-Dec-19 2:23am    
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.

The Message Shows some table name or column name is not in the correct format.Aliases defined as "" or [] are not allowed. i have no message about your database table structure.so I can do nothing for you. sorry. I suggest you check your database table structure first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900