T
T
Trianid Trianid2016-04-05 02:12:36
MySQL
Trianid Trianid, 2016-04-05 02:12:36

How to save changes in DataGrid VC#?

Goodnight! Pulled out from a database in a column. Everything is displayed well. But after editing - it is not saved naturally. How to use the example of my code to implement something like DbDataAdapter.Update (DataTable) to put the SAVE button and naturally all changes, additions and deletions were saved in MySQL. Here is the code:

private DataTable GetCats()

        {
            DataTable dt = new DataTable();

            MySqlConnectionStringBuilder mysqlCSB;
            mysqlCSB = new MySqlConnectionStringBuilder();
            mysqlCSB.Server = textBox1.Text;
            mysqlCSB.Database = textBox2.Text;
            mysqlCSB.UserID = textBox3.Text;
            mysqlCSB.Password = textBox4.Text;

            string queryString = @"SELECT blablabla FROM blablabla WHERE blabla IN (SELECT blabla FROM blablabla WHERE blablabla='blablabla')";

            using (MySqlConnection con = new MySqlConnection())
            {
                con.ConnectionString = mysqlCSB.ConnectionString;

                MySqlCommand com = new MySqlCommand(queryString, con);

                try
                {
                    con.Open();

                    using (MySqlDataReader dr = com.ExecuteReader())
                    {
                        if (dr.HasRows)
                        {
                            dt.Load(dr);
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            return dt;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DartAlex, 2016-04-05
@DartAlex

public static void SQLSendData(string ConnectionString, string CommandString)
{
    using (MySqlConnection connection = new MySqlConnection(ConnectionString))
    {
        try
        {
            MySqlCommand command = new MySqlCommand(CommandString, connection);

            command.Connection = connection;
            connection.Open();
            rowsAffected = command.ExecuteNonQuery();
        }
        catch (SqlException e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            connection.Close();
        }
    }
}

And now you need to catch the changes made, parse requests and send them to the database
Or look towards Entity Framework

A
Alexander Ananiev, 2016-04-05
@SaNNy32

Write code that saves data through a SQL query.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question