【问题标题】:Error while executing MySQL query执行 MySQL 查询时出错
【发布时间】:2012-11-04 21:56:24
【问题描述】:

我正在尝试运行此方法,但有时会出错,有时运行顺畅。我不知道出了什么问题。有什么想法吗?

public bool NewArchive(string appId, string fileUrl, long length, string thumbUrl)
{
    ConnectIfClosed();

    string query = "INSERT INTO archive (appid, file_url, thumb_url, length) " +
        "VALUES ('" + appId + "', '" + fileUrl + "', '" + thumbUrl + "', '" + length + "');";
    bool result;

    using (MySqlCommand cmd = new MySqlCommand(query, sqlConnector))
    {
        try
        {
            cmd.ExecuteNonQuery();
            result = true;
        }
        catch (Exception ex)
        {
            mysqlerror = ex.ToString();
            result = false;
        }
    }

    return result;
}

这是我有时遇到的异常 首先,我认为这与不正确处理 cmd 有关.. 但我不确定。

MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): 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()
   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, 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.ExecuteReader()
   at ScrSnap.Sql.GetRowValueFromArchive(String row, String where, String wherematch) in C:\Users\Ben\Documents\Visual Studio 2010\Projects\Image software\ScrSnap 4\ScrSnap 4\Sql.cs:line 156

【问题讨论】:

  • 尝试调试以查看出错时传递了哪些值。

标签: c# mysql exception insert


【解决方案1】:

我认为这里的问题是您与多个命令对象共享一个连接对象(我假设您的程序中的 sql 比这个还多;))。除非他们都参与单一交易,否则请避免这样做。

【讨论】:

    【解决方案2】:

    几件事...“ID”列,可能长度也是数字字段,在构建字符串时不应包含在引号中。此外,如果您曾经是基于 Web 的(或者如果内部是恶意的),那么通过为 SQL 构建一个字符串将很容易发生 SQL 注入。无论如何,我强烈建议您习惯于使用 MySQLCommand 的参数化变量。

    你有

     string query = "INSERT INTO archive (appid, file_url, thumb_url, length) " +
            "VALUES ('" + appId + "', '" + fileUrl + "', '" + thumbUrl + "', '" + length + "');";
    

    改成

     string query = "INSERT INTO archive (appid, file_url, thumb_url, length) " +
            "VALUES (" + appId + ", '" + fileUrl + "', '" + thumbUrl + "', " + length + ");";
    

    【讨论】:

      【解决方案3】:

      我在下面的语句中用 & 更改了 + ,然后它就可以正常工作了。

      string query = "INSERT INTO archive (appid, file_url, thumb_url, length) " +
              "VALUES ('" + appId + "', '" + fileUrl + "', '" + thumbUrl + "', '" + length + "');";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-30
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多