简体   繁体   English

执行MySQL查询时出错

[英]Error while executing MySQL query

Im trying to run this method but sometimes, it errors, sometimes it runs smoothly. 我试图运行这种方法,但有时,它错误,有时它运行顺利。 I can't figure out what's going wrong. 我无法弄清楚出了什么问题。 Any ideas? 有任何想法吗?

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;
}

This is the exception I get sometimes First I thought it was to do with not disposing cmd incorrectly.. but im not sure. 这是我有时会遇到的例外首先我认为这与不正确处理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

I think the problem here is that you share a connection object with multiple command objects (I assume that you have more sqls in your program than just this one ;) ). 我认为这里的问题是你与多个命令对象共享一个连接对象(我假设你的程序中有更多的sqls,而不仅仅是这个;))。 Unless they are all involved in a single transaction please avoid doing this. 除非他们都涉及单笔交易,否则请避免这样做。

couple things... "ID" columns, and probably length too are numeric fields and should NOT be enclosed in quote as you are building the string. 几个东西......“ID”列,也许长度也是数字字段,在构建字符串时不应该用引号括起来。 Additionally, by building a string for the SQL would be prone to SQL-Injection if you are ever web-based (or otherwise if malicious internally). 此外,如果您是基于Web的(或者如果内部是恶意的),通过为SQL构建字符串将倾向于SQL注入。 Anyhow, I would strongly suggest getting used to parameterized variables to your MySQLCommand. 无论如何,我强烈建议习惯参数化变量到你的MySQLCommand。

You have 你有

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

Change to 改成

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

I changed + with & on below statment , then it work fine. 我在下面的语句中用&更改了+,然后它工作正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM