database backup

0

I looked at some tutorials here on the site, but I did not find one that fits with what I'm looking for, so I followed this tutorial Backup & Restore Sql Server database in C #

It even has a link in the comments with the codes already ready, but when I click to create the DB backup it appears this error:

Idonotknowifitmakesadifference,IdonotuseSqlServerManagement,IuseVisualStudio2010directly,doesanyoneknowhowtosolvetheproblem?

PS1:

privatevoidbtn_criar_Click(objectsender,EventArgse){stringdatabase=con.Database.ToString();try{if(path_criar.Text==string.Empty){MessageBox.Show("please enter backup file location");
            }
            else
            {
                string cmd = "BACKUP DATABASE [" + database + "] TO DISK='" + path_criar.Text + "\" + "database" + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".bak'";

                using (SqlCommand command = new SqlCommand(cmd, con))
                {
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    command.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("database backup done successefully");
                    btn_criar.Enabled = false;
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

The part of line 54 is this command.ExecuteNonQuery();

PS2: I discovered that if I clicked on backup again shortly after closing the error, it backs up or restores depending on what I choose, so I "got it", put a btn_criar.PerformClick () ; so I clicked the button again instead of the error appears, but I would like to solve the problem because I do not know what problems can happen in the future (and yes, I already tested and the backup works)

    
asked by anonymous 13.02.2018 / 03:51

1 answer

0

On a Server it is common to use the SMO library to perform a backup or restore. SMO | SQL Server Management Objects contains numerous classes that make it much easier to manage a database programmatically. If you are interested, see here: Microsoft SMO Documentation

I've created a more effective and efficient example to back up a LOCAL DB.

After creating a project, I added a .mdf database to the project and left the default name Database1.mdf, added a new query, created a table, and then started with the same syntax used by you: BACKUP DATABASE - TO DISK='' . see the image below.

HereIcameacrosstwoproblems:

  • PropertyConexao.Databasereturningmenull;
  • DataTime.Now.ToString()generatingsyntaxerror;

NotethatIhaveacommentedquerynamestringwhichwasmyfirstattempt,whereIcameacrossthe2problems.

ThesolutionIfoundwastousetheApplication.StartupPath,toreturnthepathtodbbyaddingitsnamethen.the2problemthataffectedthesyntaxwerethedateandtimeseparatorsusedinthereturnformat.

stringquery=$"BACKUP DATABASE [{Application.StartupPath}\DATABASE1.MDF] TO DISK ='{textBox1.Text}\Database1_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.bak'";

Note that it worked perfectly, in the image above in the screen of the desktop folder I have 3 backup, although 2 are a little higher. every click generates a new one, I'm not afraid how to overlap since DateTime will never return the msn nanosecond.

    
13.02.2018 / 15:37