myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   C# - dataGridView => in Datenbank schreiben (https://mygully.com/showthread.php?t=2796436)

fkaustriawien 05.01.13 09:25

C# - dataGridView => in Datenbank schreiben
 
Hallo Leute!

Ich habe folgendes Problem:
- Ich möchte meine dataGridView1, die ich mittels:
--------------------------------------------------------------------------------------------------
Database.starten(); (connection string erstellen, etc...)

MySqlDataAdapter adapter = new MySqlDataAdapter();
DataTable table = new DataTable();

string abfrage = "SELECT * FROM " + comboBox1.Text + ";";

adapter = new MySqlDataAdapter(abfrage, Database.Connection);

adapter.Fill(table);

dataGridView1.DataSource = table;
--------------------------------------------------------------------------------------------------
mit dem ("Werte-Auslese - Button) erfolgreich auslese , nun wieder zurück in die Datenbank schreiben ! ( Am Besten die komplette dataGridView)

D.h: in der comboBox1 steht meine Tabelle, von der ich sie zuvor eingelesen habe.

Welcher Code gehört jetzt nun in meinen "Update Tabelle - Button"?
Jede Zeile einzeln rüberspielen? Habt ihr irgendwelche Vorschläge?
Ich find einfach nichts brauchbares im I-net

Wär mir sehr geholfen, danke!

ProgMaster 05.01.13 13:54

Es gibt kein "C# DataGridView".
C# ist eine Programmiersprache!

Du solltest also noch mitteilen, welches DataGridView Du verwendest (WF, WPF, gekaufte?).
Verwendest Du kein Model im Hintergrund, DataBinding?

Matze500 10.01.13 18:50

Hier nen kleines Bsp. was ich mal machen musste für die Schule:


Die Form:
Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Verleihshop
{
    public partial class Form_Warenverwaltung : Form
    {
        sql datenbank = new sql();
        DataTable tbl = new DataTable();
        BindingSource source = new BindingSource();

        public Form_Warenverwaltung()
        {
            InitializeComponent();
            tbl = datenbank.warenverwaltung();
            source.DataSource = tbl;
            GridView_Waren.DataSource = source;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            datenbank.setwaren(tbl);
        }
    }
}

Die SQL Klasse (mit Stored Procedures):

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Verleihshop
{
    class sql
    {
        SqlConnection connection = new SqlConnection("server=localhost\\SQLEXPRESS;" + "Trusted_Connection=yes;" + "database=db_Verleih;"); // Bei dir vielleicht anders

        public sql()
        {
        }

        public DataTable warenverwaltung()
        {
            //Stored Procedure für die Warenverwaltung

            SqlDataAdapter adapter;
            DataTable table = new DataTable();
            SqlCommand command = new SqlCommand();
            SqlCommandBuilder builder;

            try
            {
                connection.Open();

                command.Connection = connection;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "all_waren";

                adapter = new SqlDataAdapter(command);
                builder = new SqlCommandBuilder(adapter);

                adapter.Fill(table);
            }
            catch (Exception i)
            {
                Console.WriteLine(i.ToString());
            }
            finally
            {
                connection.Close();
            }

            return table;
        }

        public void setwaren(DataTable table)
        {
            SqlDataAdapter adapter;
            SqlCommand command = new SqlCommand();
            SqlCommandBuilder builder;

            try
            {
                connection.Open();

                command.Connection = connection;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "all_waren";

                adapter = new SqlDataAdapter(command);

                builder = new SqlCommandBuilder(adapter);
                adapter.Update(table);

            }
            catch (Exception i)
            {
                Console.WriteLine(i.ToString());
            }
            finally
            {
                connection.Close();
            }
        }
    }
}

Ich hoffe das Hilft dir der SqlCommandBuilder kann automatisch die Funktion ändern das du geänderte Sachen speicherst.

Ich hoffe ich konnte dir helfen, ich hoffe das ist verständlich für dich. (Selber schon 3 Jahre mit C# Programmiert)

Wenn du Lust hast schick mir ne PM dann können wa über TS oder Skype darüber reden und ich kann dich noch besser Supporten.

MFG Matze500


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:08 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.