2012-11-16 161 views
1

我試圖運行這個方法,但有時它錯誤,有時它運行平穩。我無法弄清楚發生了什麼問題。有任何想法嗎?執行MySQL查詢時出錯

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 
+0

嘗試調試以查看哪些值在出現錯誤時正在傳遞。 –

回答

3

我認爲這裏的問題是,你共享與多個命令對象(我假設你在程序不僅僅是這一個有更多的sql語句;)的連接對象)。除非他們都參與單一交易,否則請避免這樣做。

+0

這是問題,謝謝 – Ben

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 + ");"; 
0

我改+與&下面statment,那麼它做工精細。

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