gpt4 book ai didi

c# - 使用C#将xml加载到mysql

转载 作者:行者123 更新时间:2023-11-29 02:52:13 24 4
gpt4 key购买 nike

我正在使用“加载本地 xml”脚本通过 C# 将 xml 文件加载到 mysql 数据库。代码如下所示:

public void loadXML(string path, string table, string identify)
{
string query = "LOAD XML LOCAL INFILE ";
query += "'"+path+"'";
query += " INTO TABLE " + table;
query += " Rows Identified By '<" + identify + ">';";
System.Diagnostics.Debug.Print(query);
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);

//Execute command
cmd.ExecuteNonQuery();

//close connection
this.CloseConnection();
}

}

但是我无法完成这项工作。我正在使用 VS 2015。它给出了一个 mysqlException: “尝试读取结果集时遇到 fatal error 。”InnerException 是找不到文件,这很奇怪,因为该文件绝对不是我的目标文件。

此处显示调试输出:

Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.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>ConsoleApplication1.vshost.exe</AppDomain> <Exception><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>Fatal error encountered attempting to read the resultset.</Message><StackTrace> at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at ConsoleApplication1.DBConnect.loadXML() in C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\DBConnect.cs:line 124
at ConsoleApplication1.Program.Main() in C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\Program.cs:line 47
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---&amp;gt; MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---&amp;gt; MySql.Data.MySqlClient.MySqlException (0x80004005): Error during LOAD DATA LOCAL INFILE ---&amp;gt; System.IO.FileNotFoundException: Could not find file 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'.
File name: 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp;amp; affectedRow, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp;amp; affectedRows, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at ConsoleApplication1.DBConnect.loadXML() in C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\DBConnect.cs:line 124
at ConsoleApplication1.Program.Main() in C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\Program.cs:line 47
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()</ExceptionString><InnerException><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>Fatal error encountered attempting to read the resultset.</Message><StackTrace> at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)</StackTrace><ExceptionString>MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---&amp;gt; MySql.Data.MySqlClient.MySqlException (0x80004005): Error during LOAD DATA LOCAL INFILE ---&amp;gt; System.IO.FileNotFoundException: Could not find file 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'.
File name: 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp;amp; affectedRow, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp;amp; affectedRows, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)</ExceptionString><InnerException><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>Error during LOAD DATA LOCAL INFILE</Message><StackTrace> at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp;amp; affectedRow, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp;amp; affectedRows, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()</StackTrace><ExceptionString>MySql.Data.MySqlClient.MySqlException (0x80004005): Error during LOAD DATA LOCAL INFILE ---&amp;gt; System.IO.FileNotFoundException: Could not find file 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'.
File name: 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp;amp; affectedRow, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp;amp; affectedRows, Int64&amp;amp; insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()</ExceptionString><InnerException><ExceptionType>System.IO.FileNotFoundException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Could not find file 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'.</Message><StackTrace> at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)</StackTrace><ExceptionString>System.IO.FileNotFoundException: Could not find file 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'.
File name: 'C:\Users\Lu\Google Drive\Capstone\CMPUT701\Project Codes\ConsoleApplication1\ConsoleApplication1\bin\Debug\UsersLuDesktopperson.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename)</ExceptionString></InnerException></InnerException></InnerException></Exception></TraceRecord>

最佳答案

根据你的代码你必须

缓存连接字符串但不缓存连接本身。

public void loadXML(string path, string table, string identify)
{
var con = new Connection(ConnectionString)
string query = "LOAD XML LOCAL INFILE ";
query += "'"+path+"'";
query += " INTO TABLE " + table;
query += " Rows Identified By '<" + identify + ">';";
System.Diagnostics.Debug.Print(query);
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query,con );

//Execute command
cmd.ExecuteNonQuery();

//close connection
this.CloseConnection();
}

}

关于c# - 使用C#将xml加载到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34520352/

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