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

1

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

c#
mysql
.net
asked on Stack Overflow Jan 31, 2020 by Salt • edited Jan 31, 2020 by Salt

2 Answers

0

Two suggestions from me:

  1. Have you tried creating the command like this? var cmd = new MySqlCommand(SQL_update, conn); after opening the connection, then doing the cmd.ExecuteNonQuery(); ?

  2. Have you tried having the connection string looking like this instead? "SERVER=" + server + ";DATABASE=" + database + ";UID=" + username + ";PASSWORD=" + password;

... where "server" is ip/servername + port, like "myserver:3306". If server is local server, I THINK server can be "localhost" without port if the port is the standard port.

answered on Stack Overflow Jan 31, 2020 by Mats Magnem
0

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.

answered on Stack Overflow Jan 31, 2020 by Salt

User contributions licensed under CC BY-SA 3.0