加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 命令执行期间遇到致命错误

发布时间:2020-12-15 04:26:04 所属栏目:百科 来源:网络整理
导读:当我从我的c#代码执行远程 mysql上的存储过程时,我得到一般错误: “在命令执行期间遇到致命错误” 总详情: MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution. --- MySql.Data.MySqlClient.MySqlException: Fatal
当我从我的c#代码执行远程 mysql上的存储过程时,我得到一般错误:
“在命令执行期间遇到致命错误”
总详情:
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 progressn");
                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条记录),执行失败

解决方法

如果此处有多个操作,您的代码中可能存在线程安全问题,并且可能会受益于使用AsyncWaitHandle而不是轮询IAsyncResult的IsComplete属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读