执行MySQL查询时出错

时间:2012-11-16 07:20:57

标签: c# mysql exception insert

我试图运行此方法,但有时,它会出错,有时会顺利运行。我无法弄清楚出了什么问题。有什么想法吗?

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

3 个答案:

答案 0 :(得分:3)

我认为这里的问题是你与多个命令对象共享一个连接对象(我假设你的程序中有更多的sqls,而不仅仅是这个;))。除非他们都涉及单笔交易,否则请避免这样做。

答案 1 :(得分:0)

几个东西......“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 + ");";

答案 2 :(得分:0)

我用&更改了+在下面的声明,然后它工作正常。

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