Warm tip: This article is reproduced from stackoverflow.com, please click
.net c# mysql

Is there an easy fix to my code to get the mysql Update statement working in csharp?

发布于 2020-03-30 21:14:16

Good evening.

I cant get the Mysql Update Statement working. Maybe i am missing something.

The same mysqlconnection worked flawlessly to Select and Insert Data. FYI those sections(Select and Insert) are commented out.

I get following 4 error messages:

  • MySqlException: Fatal error encountered attempting to read the resultset.
  • MySqlException: Reading from the stream has failed.
  • IOException
  • SocketException
        MySqlConnection conn = DBUtils.GetDBConnection();
        conn.Open();
        MySqlCommand cmd = conn.CreateCommand();
        string SQL_update = "UPDATE discordbot.tb_lurk SET lupo = lupo + 1 WHERE twitch = '68AFCDC29BFFBDAC4F59A43AE39AFF84'";
        cmd.CommandText = SQL_update;
        cmd.ExecuteNonQuery();
        conn.Close();

Tableview in Workbench: https://gyazo.com/858f51ea25c4b3526e95c64d5b40afd2

    class DBMySQLUtils
    {

        public static MySqlConnection
                 GetDBConnection(string host, int port, string database, string username, string password)
        {
            // Connection String.
            String connString = "Server=" + host + ";Database=" + database
                + ";port=" + port + ";User Id=" + username + ";password=" + password;

            MySqlConnection conn = new MySqlConnection(connString);

            return conn;
        }

    }    
    class DBUtils
    {
        public static MySqlConnection GetDBConnection()
        {
            string host = "ip-here";
            int port = 3306;
            string database = "discordbot";
            string username = "discordbot";
            string password = "pwhere";

            return DBMySQLUtils.GetDBConnection(host, port, database, username, password);
        }

    }

Full exception:

MySql.Data.MySqlClient.MySqlException
  HResult=0x80004005
  Message=Fatal error encountered during command execution.
  Source=MySql.Data
  StackTrace:
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
   at sqltest.Program.Main(String[] args) in C:\Users\chaos\Dropbox\Visual Studio\Mysqltest\sqltest\sqltest\Program.cs:line 35

Inner Exception 1:
MySqlException: Fatal error encountered attempting to read the resultset.

Inner Exception 2:
MySqlException: Reading from the stream has failed.

Inner Exception 3:
IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Inner Exception 4:
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Thanks alot in advance

Questioner
Salt
Viewed
21
Salt 2020-02-01 03:30

Ok guys... I thank everyone single one of you who tried to help me.

I found the error. The Mysql Server was running on Version 5.7.17. I did not update the server but installed the new version of Mysql Benchmark instead on the Windowsserver 2012. I can not open the new Benchmark but it fixed the communication Problem.

I have no clue whatsoever why certain statements did work (Insert, Select) but UPDATE did not.