簡體   English   中英

執行MySQL查詢時出錯

[英]Error while executing MySQL query

我試圖運行這種方法,但有時,它錯誤,有時它運行順利。 我無法弄清楚出了什么問題。 有任何想法嗎?

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

我認為這里的問題是你與多個命令對象共享一個連接對象(我假設你的程序中有更多的sqls,而不僅僅是這個;))。 除非他們都涉及單筆交易,否則請避免這樣做。

幾個東西......“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 + ");";

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

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