gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:50:43 26 4
gpt4 key购买 nike

我使用了 following code用于备份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();
}

我的堆栈跟踪如下 -

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)
..

但是有一个异常,据说有一个空引用异常。当我通过 C# 程序将数据传递到数据库时。插入成功,无一异常(exception)。此异常仅在我尝试通过 C# 程序备份数据库时发生。我使用了上面链接中的 2 个 Dll 文件。那些是 - MySql.Data.dll MySqlBackup.dll

我无法解决这个异常。请帮忙。

Exception

最佳答案

尝试使用此功能(不要忘记您必须将 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);
}
}

关于c# - 使用 MySqlBackup.NET 导出数据库时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14929830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com