gpt4 book ai didi

c# - 命令执行期间遇到 fatal error

转载 作者:可可西里 更新时间:2023-11-01 06:42:15 25 4
gpt4 key购买 nike

当我从我的 c# 代码在远程 mysql 上执行存储过程时,我得到了一般错误:“执行命令时遇到 fatal error ”总的细节:

MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution. --->  MySql.Data.MySqlClient.MySqlException: Fatal error encountered attempting to read the resultset. --->  MySql.Data.MySqlClient.MySqlException: Reading from the stream has failed.
---> System.IO.EndOfStreamException: Attempted to read past the end of the stream. at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
--- End of inner exception stack trace --- at MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId) at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
--- End of inner exception stack trace --- at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) --- End of inner exception stack trace --- at MySql.Data.MySqlClient.MySqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult

)

我的 C# 代码:

 internal void InsertUpdateMySql(string connectionstring, string storedprocedure)
{
string sLog = "internal void InsertUpdateMySql(string connectionstring, string storedprocedure)";
MySqlConnection conn = new MySqlConnection();
int rowsAffected = 0;
try
{
if (!string.IsNullOrEmpty(storedprocedure) && !string.IsNullOrEmpty(connectionstring))
{
conn.ConnectionString = connectionstring;
MySqlCommand cmd = new MySqlCommand(storedprocedure);
cmd.Connection = conn;
conn.Open();
IAsyncResult myResult = cmd.BeginExecuteNonQuery(null, null);
SetProgressinRichText("In progress\n");
while (!myResult.IsCompleted)
{
SetProgressinRichText(".");
}
rowsAffected = cmd.EndExecuteNonQuery(myResult);
}
}
catch (MySqlException ex)
{
this.service.WriteToLog(source, sLog + "\n" + ex.Message + "\n" + ex.StackTrace);
}
catch (Exception ex)
{
this.service.WriteToLog(source, sLog + "\n" + ex.Message + "\n" + ex.StackTrace);
}
finally
{
if (conn.State != System.Data.ConnectionState.Closed)
conn.Close();
}
}

sp 也一样:

DELIMITER $$


CREATE PROCEDURE `SPInsertUpdateCity`( in SP_CityName VARCHAR(100) charset utf8, in SP_CitySynonyms varchar(100) charset utf8, in SP_CityNumberPostOffice varchar(100)) BEGIN if(not exists(select CityID from city where CityName = SP_CityName)) then insert into city(CityName, CityNumberPostOffice) values(SP_CityName, SP_CityNumberPostOffice); insert into citysynonym (CityID, CitySynonymName) values(Last_insert_id(),SP_CitySynonyms); else if(not exists( select CitySynonymID from citysynonym where CitySynonymName = SP_CitySynonyms)) then
insert into citysynonym (CityID, CitySynonymName) values((select cityid from city where CityName=SP_CityName), SP_CitySynonyms); end if; end if; END$$

DELIMITER ;

连接字符串:

Server=XXXX; Port=XXXX; Database=XXXX; Uid=XXXX;Pwd=XXXX;charset=utf8;default command timeout=7200;

但是当我在本地 mysql 上执行它时它也成功了!对于远程 mysql,我尝试发送少量数据(3 条记录)并成功执行,但对于大数据(1500 条记录)执行失败

最佳答案

当执行我的查询花费的时间超过我的 MysqlCommand CommandTimeout 默认值时,我遇到了这个问题。所以我只是增加了那个时间,之后我就没有这样的错误了。

MysqlCommand cmd = new MysqlCommand();
cmd.CommandTimeout = 200;

关于c# - 命令执行期间遇到 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5292842/

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