简体   繁体   English

使用MySqlBackup.NET导出数据库时出现异常

[英]Exception when using MySqlBackup.NET to export database

I used the following code for backup the MYSQL database. 我使用以下代码备份MYSQL数据库。

    private void button2_Click(object sender, EventArgs e)
    {
        string file = "D:\\backup.sql";
        //string conn = "server=localhost;user=root;pwd=qwerty;database=test;";
        String str = @"server=192.168.1.219;database=abc;userid=sha;password='123';";
        MySqlBackup mb = new MySqlBackup(str);
        mb.ExportInfo.FileName = file;
        mb.Export();
    }

my stack trace is following - 我的堆栈跟踪如下 -

A first chance exception of type 'System.NullReferenceException' occurred in MySqlBackup.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>TestAppMysqlDBConnect.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Object reference not set to an instance of an object.</Message><StackTrace>   at MySql.Data.MySqlClient.MySqlBackup.ExportExecute()
   at MySql.Data.MySqlClient.MySqlBackup.Export()
   at TestAppMysqlDBConnect.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Shashika\Documents\Visual Studio 2010\Projects\TestAppMysqlDBConnect\TestAppMysqlDBConnect\Form1.cs:line 52
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   ..

But there is a exception and is says there is a null references exception. 但是有一个例外,并且说有一个空引用异常。 when i pass the data to the database through the C# program. 当我通过C#程序将数据传递到数据库时。 it was successfully inserted there were no exception. 它被成功插入,没有例外。 this exception only occur in when i try to backup the database through the C# program. 此异常仅在我尝试通过C#程序备份数据库时发生。 i used 2 Dll files that was in above link. 我使用了上面链接中的2个Dll文件。 those are - MySql.Data.dll MySqlBackup.dll 那些是 - MySql.Data.dll MySqlBackup.dll

I can not solve this exception. 我无法解决这个异常。 Please help. 请帮忙。

例外

Try to use this function (Don't forget that you have to add MySqlBackup.dll to your project reference): 尝试使用此功能(不要忘记您必须将MySqlBackup.dll添加到项目参考中):

public void Backup()
    {
        try
        {
            // Backup...
            DateTime Time = DateTime.Now;
            year = Time.Year;
            month = Time.Month;
            day = Time.Day;
            hour = Time.Hour;
            minute = Time.Minute;
            second = Time.Second;
            millisecond = Time.Millisecond;

            //Save file to Path with the current date as a filename
            string path;
            path = txb_Path.Text + year + "-" + month + "-" + day + "--" + hour + "-" + minute + "-" + second + ".sql";
            string file = path;
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ExportToFile(file);
                        conn.Close();
                    }
                }
            }
            //Done----
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error , unable to backup!" + ex.Message);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySqlBackup.NET与MySql.Data版本有关的问题 - MySqlBackup.NET issue with MySql.Data version MySqlBackup.NET QueryExpress.ExecuteScalarStr()输出用双引号而不是反引号引起来的字符串 - MySqlBackup.NET QueryExpress.ExecuteScalarStr() outputs strings enclosed in double-quotes instead of backticks 使用mysqlBackup备份数据库时拒绝访问路径 - Access path denied when backing up database using mysqlBackup 将Massive与SQLite一起使用时,“数据库被锁定”异常 - “Database is locked” exception when using Massive with SQLite 如何使用 C# (MySqlBackup.dll) 在 mysql 中解密、incryptdumpfile - How to Decrypt, incryptdumpfile in mysql using C# (MySqlBackup.dll) 在.NET 4中使用IntPtr时出现PInvokeStackImbalance异常? (适用于.NET 3.5) - PInvokeStackImbalance exception when using IntPtr in .NET 4? (Works in .NET 3.5) 尝试使用asp.net更新数据库时出现异常错误 - Exception Error in trying to update database using asp.net 使用asp.net mvc导出到excel函数时,带有响应对象的nullreference异常 - nullreference exception with reponse object in export to excel function using asp.net mvc 即使使用C#.NET中的OleDbConnection类从DB2数据库中没有引发异常,也如何获取SQLCode - How to get SQLCode even when no exception is thrown from DB2 database using OleDbConnection class in C#.NET 使用 EF 时数据库连接丢失时如何捕获异常? - How to catch exception when database connection is lost when using EF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM