N
N
NeoLight22020-11-15 15:41:29
SQL
NeoLight2, 2020-11-15 15:41:29

Why can't I change the value of a SQL field in C#?

For some reason, I can’t change the field in SQL, I tried with and without stubs, zero. Here is the code:

public static void Main(string[] args)
        {
            const string info = "Server=localhost;Database=unturned2;Uid=root;pwd=root;charset=utf8";
            MySqlConnection mycon = new MySqlConnection(info);
            const string insert = "INSERT INTO wallets (steamid,balance) VALUES (@id , @bal)";


            try
            { 
                mycon.Open();
                Console.WriteLine("Successfully connected!");
                MySqlCommand command = new MySqlCommand(insert, mycon);
                command.Parameters.Add("@id", MySqlDbType.VarChar).Value = "76561198928736885";
                command.Parameters.Add("@bal", MySqlDbType.Double).Value = 500;
                if(command.ExecuteNonQuery() != 1)
                {
                    Console.WriteLine("Error");
                }
                MySqlCommand changebal = new MySqlCommand("UPDATE wallets SET balance=0 WHERE steamid=76561198928736885", mycon);
                if(changebal.ExecuteNonQuery() != 1)
                {
                    Console.WriteLine("Error occured with changing");
                }

            }
            catch(Exception ex)
            {
                Console.WriteLine($"Error occured, {ex.Message}");
            }
            finally
            {
                mycon.Close();
            }
        }

5fb121f6e859e455168663.png
Please help, thanks in advance :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-11-15
@firedragon

https://dapper-tutorial.net/

string sql = "INSERT INTO wallets (steamid,balance) VALUES (@id , @bal);";

using (var connection = new SqlConnection(info))
{
      var affectedRows = connection.Execute(sql, new {id, bal});
      Console.WriteLine(affectedRows);
     sql = "UPDATE wallets SET balance=0 WHERE [email protected]";
     var id =76561198928736885;
     var affectedRows = connection.Execute(sql, new {id});
}

O
os9, 2020-11-16
@os9

1. Enclose the string field in quotation marks (is it a string field?), Be careful with types.
2. Copy-paste both of your queries and execute manually somewhere in the application for working with the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question