Pazi kad me nije mrzelo!
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace mysql
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components = null;
MySqlConnection con; // objekat veze
DataSet podaci = new DataSet(); //data set da cuva podatke
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
string connection = "Data Source=localhost;Database=test;User Id=;Password="; //connection string, postavi na ono sto ti treba
public Form1()
{
InitializeComponent();
con = new MySqlConnection(connection); // inicijalizujemo vezu
con.Open();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(536, 248);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 288);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Ucitaj tabelu";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(448, 288);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.TabIndex = 2;
this.button2.Text = "Snimi";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(568, 341);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
// ovo dugme ucitava tabelu i vezuje je sa datagrid kontrolom
MySqlDataAdapter da = new MySqlDataAdapter("select * from test",con);
da.Fill(podaci);
dataGrid1.DataSource = podaci.Tables[0];
}
private void button2_Click(object sender, System.EventArgs e)
{
// ovo dugme izmene vraca u bazu.
MySqlDataAdapter da = new MySqlDataAdapter("select * from test",con);
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
da.Update(podaci);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
con.Close();
con.Dispose();
}
}
}
Mozda je ovo malo navrat nanos, ali sve se razume naravno!
Lepota je u jednostavnosti.
Cis.