C# 执行MySQL查询时出错

C# 执行MySQL查询时出错,c#,mysql,exception,insert,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 ('

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

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

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

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

两件事。。。“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 + ");";
两件事。。。“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 + "');";

我在下面的状态下更改了+和,然后它就可以正常工作了

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

尝试调试以查看在它出错时传递的值。尝试调试以查看在它出错时传递的值。