Visual C#.NET'te Veritabanı İşlemleri - 2
Visual C#.NET de Veritabanı İşlemleri-2
-------------------------------------------------------------------------------- Veritabanımızda ekle-sil-yenile-bul-güncelle-exel'e gönder işlemlerini yapmamızı sağlar. using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Runtime.InteropServices; using Office = Microsoft.Office.Core; namespace ocx { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.TextBox textBox5; private System.Windows.Forms.TextBox textBox6; private System.Windows.Forms.TextBox textBox7; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.Button button1; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button temizle; private System.Windows.Forms.Button sil; private System.Windows.Forms.Button kayit; private System.Windows.Forms.Button bul; private System.Windows.Forms.Button güncelle; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button4; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typ eof(Form1)); this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.textBox5 = new System.Windows.Forms.TextBox(); this.textBox6 = new System.Windows.Forms.TextBox(); this.textBox7 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.button4 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.temizle = new System.Windows.Forms.Button(); this.sil = new System.Windows.Forms.Button(); this.kayit = new System.Windows.Forms.Button(); this.bul = new System.Windows.Forms.Button(); this.güncelle = new System.Windows.Forms.Button(); this.timer1 = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).BeginInit(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Info; this.dataGrid1.CaptionBackColor = System.Drawing.Color.LightSkyBlue; this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.CaptionText = " KAYITLI PERSONELLER"; this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(8 104); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(520 160); this.dataGrid1.TabIndex = 0; this.dataGrid1.DoubleClick += new System.EventHandler(this.dataGrid1_DoubleClick); this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChan ged); this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dat aGrid1_Navigate); // // textBox1 // this.textBox1.BackColor = System.Drawing.SystemColors.Info; this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif" 9F System.Drawing.FontStyle.Bold System.Drawing.GraphicsUnit.Point ((byte)(162))); this.textBox1.ForeColor = System.Drawing.Color.Red; this.textBox1.Location = new System.Drawing.Point(96 8); this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(72 21); this.textBox1.TabIndex = 2; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(96 32); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(72 20); this.textBox2.TabIndex = 3; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(96 56); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(144 20); this.textBox3.TabIndex = 4; // // textBox4 // this.textBox4.Location = new System.Drawing.Point(96 80); this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(144 20); this.textBox4.TabIndex = 5; // // textBox5 // this.textBox5.Location = new System.Drawing.Point(336 8); this.textBox5.Name = "textBox5"; this.textBox5.Size = new System.Drawing.Size(144 20); this.textBox5.TabIndex = 6; // // textBox6 // this.textBox6.Location = new System.Drawing.Point(336 32); this.textBox6.Name = "textBox6"; this.textBox6.Size = new System.Drawing.Size(56 20); this.textBox6.TabIndex = 7; // // textBox7 // this.textBox7.Location = new System.Drawing.Point(336 56); this.textBox7.Name = "textBox7"; this.textBox7.Size = new System.Drawing.Size(136 20); this.textBox7.TabIndex = 8; // // label1 // this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label1.Location = new System.Drawing.Point(8 32); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(80 20); this.label1.TabIndex = 13; this.label1.Text = "Sicil"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // label2 // this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label2.Location = new System.Drawing.Point(8 56); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(80 20); this.label2.TabIndex = 14; this.label2.Text = "Adı"; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // label3 // this.label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label3.Location = new System.Drawing.Point(8 80); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(80 20); this.label3.TabIndex = 15; this.label3.Text = "Soyadı"; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // label4 // this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label4.Location = new System.Drawing.Point(248 8); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(80 20); this.label4.TabIndex = 16; this.label4.Text = "Ünvan"; this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // label5 // this.label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label5.Location = new System.Drawing.Point(248 32); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(80 20); this.label5.TabIndex = 17; this.label5.Text = "Hizmet Süresi"; this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // label6 // this.label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label6.Location = new System.Drawing.Point(248 56); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(80 20); this.label6.TabIndex = 18; this.label6.Text = "Servis"; this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // label7 // this.label7.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.label7.Location = new System.Drawing.Point(8 8); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(80 20); this.label7.TabIndex = 19; this.label7.Text = "Sıra No"; this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // button1 // this.button1.BackColor = System.Drawing.Color.AliceBlue; this.button1.Location = new System.Drawing.Point(80 64); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(104 24); this.button1.TabIndex = 20; this.button1.Text = "Excel\'e Gönder"; this.button1.UseVisualStyleBackColor = false; this.button1.Click += new System.EventHandler(this.button1_Click); // // groupBox1 // this.groupBox1.BackColor = System.Drawing.SystemColors.InactiveCaption; this.groupBox1.Controls.Add(this.button4); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.temizle); this.groupBox1.Controls.Add(this.sil); this.groupBox1.Controls.Add(this.kayit); this.groupBox1.Controls.Add(this.bul); this.groupBox1.Controls.Add(this.güncelle); this.groupBox1.Controls.Add(this.button1); this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.groupBox1.Location = new System.Drawing.Point(96 280); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(432 96); this.groupBox1.TabIndex = 22; this.groupBox1.TabStop = false; // // button4 // this.button4.BackColor = System.Drawing.Color.Bisque; this.button4.Location = new System.Drawing.Point(8 16); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(64 24); this.button4.TabIndex = 27; this.button4.Text = "Yenile"; this.button4.UseVisualStyleBackColor = false; this.button4.Click += new System.EventHandler(this.button4_Click); // // button2 // this.button2.BackColor = System.Drawing.Color.AliceBlue; this.button2.Location = new System.Drawing.Point(216 64); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(112 24); this.button2.TabIndex = 26; this.button2.Text = "Excel\'den Al"; this.button2.UseVisualStyleBackColor = false; this.button2.Click += new System.EventHandler(this.button2_Click); // // temizle // this.temizle.BackColor = System.Drawing.Color.Bisque; this.temizle.Location = new System.Drawing.Point(80 16); this.temizle.Name = "temizle"; this.temizle.Size = new System.Drawing.Size(64 24); this.temizle.TabIndex = 17; this.temizle.Text = "Temizle"; this.temizle.UseVisualStyleBackColor = false; this.temizle.Click += new System.EventHandler(this.temizle_Click); // // sil // this.sil.BackColor = System.Drawing.Color.Bisque; this.sil.Location = new System.Drawing.Point(368 16); this.sil.Name = "sil"; this.sil.Size = new System.Drawing.Size(56 24); this.sil.TabIndex = 16; this.sil.Text = "Sil"; this.sil.UseVisualStyleBackColor = false; this.sil.Click += new System.EventHandler(this.sil_Click); // // kayit // this.kayit.BackColor = System.Drawing.Color.Bisque; this.kayit.Location = new System.Drawing.Point(296 16); this.kayit.Name = "kayit"; this.kayit.Size = new System.Drawing.Size(64 24); this.kayit.TabIndex = 15; this.kayit.Text = "Kayıt"; this.kayit.UseVisualStyleBackColor = false; this.kayit.Click += new System.EventHandler(this.kayit_Click); // // bul // this.bul.BackColor = System.Drawing.Color.Bisque; this.bul.Location = new System.Drawing.Point(152 16); this.bul.Name = "bul"; this.bul.Size = new System.Drawing.Size(64 24); this.bul.TabIndex = 14; this.bul.Text = "Bul"; this.bul.UseVisualStyleBackColor = false; this.bul.Click += new System.EventHandler(this.bul_Click); // // güncelle // this.güncelle.BackColor = System.Drawing.Color.Bisque; this.güncelle.Location = new System.Drawing.Point(224 16); this.güncelle.Name = "güncelle"; this.güncelle.Size = new System.Drawing.Size(64 24); this.güncelle.TabIndex = 13; this.güncelle.Text = "Güncelle"; this.güncelle.UseVisualStyleBackColor = false; this.güncelle.Click += new System.EventHandler(this.güncelle_Click); // // timer1 // this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5 13); this.BackColor = System.Drawing.SystemColors.Control; this.ClientSize = new System.Drawing.Size(536 381); this.Controls.Add(this.groupBox1); this.Controls.Add(this.label7); this.Controls.Add(this.label6); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox7); this.Controls.Add(this.textBox6); this.Controls.Add(this.textBox5); this.Controls.Add(this.textBox4); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.dataGrid1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this. Icon"))); this.IsMdiContainer = true; this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).EndInit(); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } OleDbConnection odc; OleDbDataAdapter adp; DataSet dt; OleDbCommandBuilder cb;// OleDbDataAdapter'i silme güncelleme ve kayıt'a hazır hale getirmek için private void Form1_Load(object sender System.EventArgs e) { this.Text = " ** Hazırlayan: Ahmet Ali Süzen** "; timer1.Interval = 100; timer1.Enabled = true; string baglanti = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath.ToString() + "\\Kutuk.mdb"; string sorgu="SELECT * FROM IZIN"; odc = new OleDbConnection(baglanti); adp = new OleDbDataAdapter(sorguodc); dt=new DataSet(); adp.Fill(dt "IZIN"); dataGrid1.ReadOnly=true; dataGrid1.CaptionText="KAYITLI PERSONELLER"; dataGrid1.DataSource = dt.Tables[0]; } private void dataGrid1_DoubleClick(object sender System.EventArgs e) { textBox1.DataBindings.Clear(); textBox2.DataBindings.Clear(); textBox3.DataBindings.Clear(); textBox4.DataBindings.Clear(); textBox5.DataBindings.Clear(); textBox6.DataBindings.Clear(); textBox7.DataBindings.Clear(); textBox1.DataBindings.Add("Text" dt.Tables[0]"ID"); textBox2.DataBindings.Add("Text" dt.Tables[0]"Sicil"); textBox3.DataBindings.Add("Text" dt.Tables[0]"Adi"); textBox4.DataBindings.Add("Text" dt.Tables[0]"Soyadi"); textBox5.DataBindings.Add("Text" dt.Tables[0]"Unvan"); textBox6.DataBindings.Add("Text" dt.Tables[0]"HizmetSuresi"); textBox7.DataBindings.Add("Text" dt.Tables[0]"Servis"); textBox2.Focus(); } private void güncelle_Click(object sender System.EventArgs e) { CurrencyManager sonraki= (CurrencyManager)this.BindingContext[dt.Tables["IZIN"]]; sonraki.EndCurrentEdit(); cb=new OleDbCommandBuilder(adp); // OleDbDataAdapter'i silme güncelleme ve kayıt'a hazır hale getirmek için adp.Update(dt "IZIN"); dt.Clear(); adp.Fill(dt.Tables[0]); dataGrid1.DataSource = dt.Tables[0]; MessageBox.Show (" Güncelleme tamamlandı!" "İŞLEM TAMAM"); } private void bul_Click(object sender System.EventArgs e) { int kayit=0; CurrencyManager yonet= (CurrencyManager)this.BindingContext[dt.Tables["IZIN"]]; foreach(DataRow satir in dt.Tables[0].Rows) { if(satir["Sicil"].ToString()==textBox2.Text) { yonet.Position = kayit; textBox1.DataBindings.Clear(); textBox2.DataBindings.Clear(); textBox3.DataBindings.Clear(); textBox4.DataBindings.Clear(); textBox5.DataBindings.Clear(); textBox6.DataBindings.Clear(); textBox7.DataBindings.Clear(); textBox1.DataBindings.Add("Text" dt.Tables[0]"ID"); textBox2.DataBindings.Add("Text" dt.Tables[0]"Sicil"); textBox3.DataBindings.Add("Text" dt.Tables[0]"Adi"); textBox4.DataBindings.Add("Text" dt.Tables[0]"Soyadi"); textBox5.DataBindings.Add("Text" dt.Tables[0]"Unvan"); textBox6.DataBindings.Add("Text" dt.Tables[0]"HizmetSuresi"); textBox7.DataBindings.Add("Text" dt.Tables[0]"Servis"); textBox2.Focus(); return; } kayit++; if(kayit>dt.Tables["IZIN"].Rows.Count-1) { MessageBox.Show (" Aradığınız SİCİL NO'lu personele ait izin kaydı bulunamadı!"); textBox1.Focus(); } } } private void kayit_Click(object sender System.EventArgs e) { System.Data.DataRow ekle; cb=new OleDbCommandBuilder(adp); // OleDbDataAdapter'i silme güncelleme ve kayıt'a hazır hale getirmek için ekle=dt.Tables["IZIN"].NewRow(); ekle["Sicil"]=textBox2.Text; ekle["Adi"]=textBox3.Text; ekle["Soyadi"]=textBox4.Text; ekle["Unvan"]=textBox5.Text; ekle["HizmetSuresi"]=textBox6.Text; ekle["Servis"]=textBox7.Text; dt.Tables["IZIN"].Rows.Add(ekle); adp.Update(dt "IZIN"); dt.Clear(); adp.Fill(dt.Tables[0]); dataGrid1.DataSource = dt.Tables[0]; MessageBox.Show (" Kayıt tamamlandı!" "İŞLEM TAMAM"); } private void sil_Click(object sender System.EventArgs e) { CurrencyManager sonraki= (CurrencyManager)this.BindingContext[dt.Tables["IZIN"]]; DialogResult uyar; uyar=MessageBox.Show(this textBox2.Text +" Sicil'li Personel Kaydını Silmek istiyor musunuz?" "SİLME UYARISI" MessageBoxButtons.YesNo MessageBoxIcon.Stop MessageBoxDefaultButton.Button2); if(uyar==DialogResult.Yes) { cb=new OleDbCommandBuilder(adp); // OleDbDataAdapter'i silme güncelleme ve kayıt'a hazır hale getirmek için sonraki.RemoveAt(sonraki.Position); adp.Update(dt "IZIN"); dt.Clear(); adp.Fill(dt.Tables[0]); dataGrid1.DataSource = dt.Tables[0]; MessageBox.Show (" Kayıt silindi!" "İŞLEM TAMAM"); } else { MessageBox.Show(" Silme işlemi İptal edildi!"); } } private void temizle_Click(object sender System.EventArgs e) { textBox1.DataBindings.Clear(); textBox2.DataBindings.Clear(); textBox3.DataBindings.Clear(); textBox4.DataBindings.Clear(); textBox5.DataBindings.Clear(); textBox6.DataBindings.Clear(); textBox7.DataBindings.Clear(); textBox1.Text=""; textBox2.Text=""; textBox3.Text=""; textBox4.Text=""; textBox5.Text=""; textBox6.Text=""; textBox7.Text=""; dt.Clear(); adp.Fill(dt.Tables[0]); dataGrid1.DataSource = dt.Tables[0]; textBox2.Focus(); } private void dataGrid1_CurrentCellChanged(object sender System.EventArgs e) { //dataGridde tıkladığımız satırın tümünün seçilmesi için dataGrid1.UnSelect (dataGrid1.CurrentRowIndex ); int i=dataGrid1.CurrentRowIndex ; try { dataGrid1.Select (i); } catch { dataGrid1.Select (); } } private void kapandı2(object sender System.EventArgs e ) { f2 = null; } ExcelExport f2; private void button2_Click(object sender System.EventArgs e) { if (f2 == null) //form değişkeni bir nesneyi göstermiyorsa { f2 = new ExcelExport(); //Yeni form oluştur f2.Closed += new EventHandler(kapandı2); f2.Text="EXCEL SAYFASINDAN VERİ ÇEKME / VERİTABANINA İŞLEME"; f2.Show(); } else //form zaten varsa öne getir f2.Text="EXCEL SAYFASINDAN VERİ ÇEKME / VERİTABANINA İŞLEME"; f2.Activate(); } private void button4_Click(object sender System.EventArgs e) { dt.Clear(); adp.Fill(dt.Tables[0]); dataGrid1.DataSource = dt.Tables[0]; } private void button1_Click(object sender System.EventArgs e) { Excel.Application uygulama = new Excel.Application(); Excel.Workbook kitap=uygulama.Workbooks.Add(true); Excel.Worksheet sayfa=(Excel.Worksheet) uygulama.ActiveSheet; uygulama.Visible=true; int rowIndex=1; int colIndex=0; foreach(DataColumn col in dt.Tables[0].Columns) { colIndex++; sayfa.Cells[1 colIndex]=col.ColumnName; } foreach(DataRow row in dt.Tables[0].Rows) { rowIndex++; colIndex=0; foreach(DataColumn col in dt.Tables[0].Columns) { colIndex++; sayfa.Cells[rowIndexcolIndex]= row[col.ColumnName].ToString(); } } } private void timer1_Tick(object sender System.EventArgs e) { this.Text = this.Text.Substring(1) + this.Text.Substring(01); } private void dataGrid1_Navigate(object sender NavigateEventArgs ne) { } } } |
Bütün Zaman Ayarları WEZ +3.5 olarak düzenlenmiştir. şu Anki Saat: 01:48 AM . |