Should I use ExecuteNonQuery for this db backup co

2020-03-31 04:59发布

I have a method that allows me to kick off a back up of a data base. What I am wondering is if I should be using ExecuteNonQuery() in this context or if there is something better to use. Here is my code currently:

    public static void RunBackup(string dbName, string filePath, string backupName, string connString)
    {
        using(SqlConnection objConnection = new SqlConnection(connString))
        {

            string commmandText = "BACKUP DATABASE @DBName TO  DISK = @FilePath WITH NOFORMAT, NOINIT, NAME = @BackUpName, SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
            SqlCommand objCommand = new SqlCommand(commmandText,objConnection);
            objCommand.Parameters.AddWithValue("@dbName", dbName);
            objCommand.Parameters.AddWithValue("@FilePath", filePath);
            objCommand.Parameters.AddWithValue("@BackUpName", backupName);

            objConnection.Open();
            objCommand.ExecuteNonQuery();
            objConnection.Close();
        }
    }

The one thing I am concerned about is being able to verify that the backup is complete and successful while handling time out issues for backups that take and extended time to complete.

标签: c# ado.net
8条回答
▲ chillily
2楼-- · 2020-03-31 05:52

To handle the issue of the long running query I ended up going with this:

    public static void RunBackup(string dbName, string filePath, string backupName, string connString)
    {
        string commmandText = "BACKUP DATABASE @DBName TO  DISK = @FilePath WITH NOFORMAT, NOINIT, NAME = @BackUpName, SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
        SqlConnection objConnection = new SqlConnection(connString);
        try
        {
            SqlCommand objCommand = new SqlCommand(commmandText, objConnection);
            objCommand.Parameters.AddWithValue("@dbName", dbName);
            objCommand.Parameters.AddWithValue("@FilePath", filePath);
            objCommand.Parameters.AddWithValue("@BackUpName", backupName);

            objConnection.Open();

            IAsyncResult result = objCommand.BeginExecuteNonQuery();
            while (!result.IsCompleted)
            {
                System.Threading.Thread.Sleep(100);
            }


            int count = objCommand.EndExecuteNonQuery(result);
         }
        catch (SqlException e)
        {
            throw e;
        }
        finally
        {
            objConnection.Close();

        }

    }

This will allow me to execute the command without asyncronously without timeout issues. I will be adding some additional error handling etc in my final code set. I may do some additional work to see if I can get a better status returned at the end of the script that I can get via EndExecuteNonQuery or through an AsyncCallBack.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-03-31 05:52

Try it. it resolved timeout expired problem while large size db.

Private Sub Command1_Click() On Error Resume Next Dim con As New Connection Dim tm As String con.CommandTimeout = 500'''Command timeout should be 500

With con .ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=dbiBMS;Data Source=192.168.103.4" .Open End With tm = CStr(Time)

con.Execute " backup database dbiBMS to disk='E:\Database_Backup\Test1.bak' with format "

con.Close MsgBox tm Exit Sub x: MsgBox Err.Description

End Sub

查看更多
登录 后发表回答