1

我试图运行此方法,但有时会出错,有时运行平稳。我不知道出了什么问题。有任何想法吗?

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
4

3 回答 3

4

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

于 2012-11-16T08:07:23.647 回答
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 + ");";
于 2012-11-16T08:12:11.493 回答
0

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

string query = "INSERT INTO archive (appid, file_url, thumb_url, length) " +
        "VALUES ('" + appId + "', '" + fileUrl + "', '" + thumbUrl + "', '" + length + "');";
于 2013-10-24T00:45:37.513 回答