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