0% found this document useful (0 votes)
39 views

C Access

The document discusses connecting to and interacting with Access and SQL databases from a C# application. It includes code to open a connection, insert, update, and delete records from databases using parameters. It also contains code to draw shapes on a form by clicking the mouse based on selected color and shape options from dropdowns. The shapes drawn and their counts are tracked and displayed on labels.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

C Access

The document discusses connecting to and interacting with Access and SQL databases from a C# application. It includes code to open a connection, insert, update, and delete records from databases using parameters. It also contains code to draw shapes on a form by clicking the mouse based on selected color and shape options from dropdowns. The shapes drawn and their counts are tracked and displayed on labels.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

C# ta Access Veritabanı Bağlantısı

Oluşturma, Veri Ekleme, Okuma ve


Düzenleme
Access Database Connection

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;

namespace wnd_form1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection baglantiyolu = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=uyg1.mdb");
DataSet ds = new DataSet();
OleDbDataAdapter adtr = new OleDbDataAdapter();

private void button1_Click(object sender, EventArgs e)


{

OleDbCommand kayit = new OleDbCommand();


kayit.CommandText = "INSERT INTO uyg1 (Alan1, Alan2, Alan3) Values (@Alan1, @Alan2, @Alan3)";
kayit.Connection = baglantiyolu;
kayit.Parameters.AddWithValue("Alan1", textBox1.Text);
kayit.Parameters.AddWithValue("Alan2", textBox2.Text);
kayit.Parameters.AddWithValue("Alan3", textBox3.Text);
kayit.ExecuteNonQuery();
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
}

private void Form1_Load(object sender, EventArgs e)


{
baglantiyolu.Open();
OleDbDataAdapter adtr = new OleDbDataAdapter("select * from uyg1", baglantiyolu);
adtr.Fill(ds, "uyg1");
dataGridView1.DataSource = ds.Tables["uyg1"];
adtr.Dispose();
baglantiyolu.Close();
}
SQL Database Connection

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace wnd_form1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
insert

private void button1_Click(object sender, EventArgs e)


{

string constring="datasource=localhost;port=3306;username=root;password=root";
string Query="insert into database.edata(Eid,name,surname,age) values(‘" + this.Eid_txt.Text + "’ ,‘" +
this.Name_txt.Text + "’ , ‘" + this.Surname_txt.Text + "’ , ‘" + this.Age_txt.Text + "’ ); " ;
MySqlConnection conDataBase=new MySqlConnection(constring);
MySqlCommand cmdDatabase=new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;

try {
conDataBase.Open();
myReader=cmdDataBase.ExecuteReader();
MessageBox.Show("Saved");
while(myReader.Read()){

} catch(Exception ex) {

MessageBox.Show(ex.Message);
}
Update

private void button2_Click(object sender, EventArgs e)


{

string constring="datasource=localhost;port=3306;username=root;password=root";
string Query="update database.edata set Eid=‘" + this.Eid_txt.Text + "’ ,name=‘" + this.Name_txt.Text + "’ ,
surname=‘" + this.Surname_txt.Text + "’ , age=‘" + this.Age_txt.Text + "’ where Eid= ‘" + this.Eid_txt.Text + "’ ; " ;
MySqlConnection conDataBase=new MySqlConnection(constring);
MySqlCommand cmdDatabase=new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;

try {
conDataBase.Open();
myReader=cmdDataBase.ExecuteReader();
MessageBox.Show("Saved");
while(myReader.Read()){

} catch(Exception ex) {

MessageBox.Show(ex.Message);
}
Delete

private void button3_Click(object sender, EventArgs e)


{

string constring="datasource=localhost;port=3306;username=root;password=root";
string Query="delete from database.edata where Eid=‘" + this.Eid_txt.Text + "’ ; " ;
MySqlConnection conDataBase=new MySqlConnection(constring);
MySqlCommand cmdDatabase=new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;

try {
conDataBase.Open();
myReader=cmdDataBase.ExecuteReader();
MessageBox.Show("Updated");
while(myReader.Read()){

} catch(Exception ex) {

MessageBox.Show(ex.Message);
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
Graphics g = CreateGraphics();
Pen p=new Pen(Color.White);
Pen p1 = new Pen(Color.Red);
Pen p2 = new Pen(Color.Green);
Pen p3 = new Pen(Color.Blue);
Pen p4 = new Pen(Color.Yellow);

if (renk == "Kırmızı") p=p1;


if (renk == "Yeşil") p=p2;
if (renk == "Mavi") p=p3;
if (renk == "Sarı") p=p4;
Rectangle myRectangle1 = new Rectangle(e.X, e.Y, 50, 50);
Rectangle myRectangle2 = new Rectangle(e.X, e.Y, 100, 50);
if (sekil == "Daire") { g.DrawEllipse(p, myRectangle1); datop++; label3.Text = Convert.ToString(datop); }
if (sekil == "Kare") {g.DrawRectangle(p, myRectangle1);katop++; label4.Text = Convert.ToString(katop);}
if (sekil == "Elips") {g.DrawEllipse(p, myRectangle2);eltop++; label5.Text = Convert.ToString(eltop);}
if (sekil == "Dikdörtgen") {g.DrawRectangle(p, myRectangle2);ditop++; label6.Text = Convert.ToString(ditop);}
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text = "Kırmızı";
comboBox1.Items.Add("Kırmızı");
comboBox1.Items.Add("Yeşil");
comboBox1.Items.Add("Mavi");
comboBox1.Items.Add("Sarı");

comboBox2.Text = "Daire";
comboBox2.Items.Add("Daire");
comboBox2.Items.Add("Kare");
comboBox2.Items.Add("Elips");
comboBox2.Items.Add("Dikdörtgen");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
renk = comboBox1.Text;
label1.Text = renk;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
sekil = comboBox2.Text;
label2.Text = sekil;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 goster = new Form2();
goster.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}

You might also like