Insert in MySQL-Datenbank



  • Hallo,
    habe mir ein kleines Beispielprogramm geschrieben, welches Daten aus einer MySQL-Datenbank abruft und in einem Datagrid darstellt. Das habe ich nach folgendem Schema gemacht:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using MySql.Data.MySqlClient;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
    
            MySqlDataAdapter adapter = new MySqlDataAdapter();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void GetData(string selectCommand)
            {
                try
                {
    
                    string connStr = "server=localhost;User Id=root;password=test123;database=testdb";
    
                    // Create a new data adapter based on the specified query.  
                    //MySqlDataAdapter adapter = new MySqlDataAdapter(selectCommand, connStr);
                    adapter = new MySqlDataAdapter(selectCommand, connStr);
                    // Create a command builder to generate SQL update, insert, and
                    // delete commands based on selectCommand. These are used to
                    // update the database.
                    MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(adapter);
                    // Populate a new data table and bind it to the BindingSource.
                    DataTable table = new DataTable();
                    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                    adapter.Fill(table);
                    bindingSource1.DataSource = table;  
    
                }
                catch (MySqlException)
                {
                    MessageBox.Show("Connection Error");
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Bind the DataGridView to the BindingSource
                // and load the data from the database.
                dataGridView1.DataSource = bindingSource1;
                GetData("select * from werkstatt");
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            }
        }
    
    }
    

    Jetzt möchte ich per Button einen Insert ausführen. Ich möchte erreichen, dass es natürlich die Tabelle in der Datenbank und auch das datagrid aktualisiert. Wie muss ich das anstellen?



  • Keiner der helfen kann? 😞



  • Ich habe mich noch nie mit dem DataGrid-Control beschäftigt, aber das Hinzufügen einer neuen Zeile in der Datentabelle läuft so ab:

    var newRow = dataTable.NewRow();
    dataTable.Rows.Add(newRow);
    newRow[..] = bla bla;    //Werte setzen
    

    Beim Speichern bist du mit einem CommandBuilder schon richtig:

    using (var cmdBuilder = new MySqlCommandBuilder()) {
        cmdBuilder.DataAdapter = dataAdapter;
        cmdBuilder.DataAdapter.Update(dataTable);
    }
    

Anmelden zum Antworten