ASP.NET MySQL Kayıt Ekleme
ASP.NET örneklerimize Mysql veri tabanına kayıt ekleme işlemi ile devam ediyoruz. İlk olarak kullanıcının bilgi giriş yapacağı form tasarımını oluşturalım
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>www.aspnetornekleri.com</title> </head> <body> <form id="form1" runat="server"> <asp:Panel id="kayitEklemePaneli" runat="server"> <table width="353" border="1"> <tbody> <tr> <td width="102"> <asp:Label id="Label1" runat="server" text="Öğrenci No"></asp:Label></td> <td width="235"> <asp:TextBox id="txtono" runat="server" Width="79px"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label id="Label2" runat="server" text="Adı"></asp:Label></td> <td> <asp:TextBox id="txtadi" runat="server" Width="177px"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label id="Label3" runat="server" text="Soyadı"></asp:Label></td> <td> <asp:TextBox id="txtsoyadi" runat="server" Width="155px"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label id="Label4" runat="server" text="Doğum Yeri"></asp:Label></td> <td> <asp:TextBox id="txtdyeri" runat="server" Width="152px"></asp:TextBox> </td> </tr> </tbody> </table> <br /> <asp:Button id="btnkaydet" runat="server" Text="Kaydet" onclick="btnkaydet_Click"></asp:Button> <br /> </asp:Panel> <asp:Label id="lblSonuc" runat="server" visible="False"></asp:Label> </form> </body> </html>
Şimdi sıra geldi kaydet butonuna basılınca çalıştıracağımız kodlara kodların sağlıklı bir şekilde çalışabilmesi için
using MySql.Data.MySqlClient;
using System.Data; namespacelerini eklemeyi unutmuyoruz ve kaydet butonuna tıklayıp aşağıdaki kodları yazıyoruz..
MySqlConnection baglanti = new MySqlConnection(); MySqlCommand komut = new MySqlCommand(); String baglanticumlesi, sorgu; //*** http://www.aspnetornekleri.com ***// baglanticumlesi = "Server=localhost;User Id=root; Password=123456; Database=okul; Pooling=false"; sorgu = "INSERT INTO ogrenci (ono,adi,soyadi,dyeri) " + " VALUES " + " ('" + this.txtono.Text + "','" + this.txtadi.Text + "','" + this.txtsoyadi.Text + "', " + " '" + this.txtdyeri.Text +"')"; baglanti.ConnectionString = baglanticumlesi; baglanti.Open(); komut.Connection = baglanti; komut.CommandText = sorgu; komut.CommandType = CommandType.Text; //*** http://www.aspnetornekleri.com ***// this.kayitEklemePaneli.Visible = false; try { komut.ExecuteNonQuery(); this.lblSonuc.Text = "Kayıt Eklendi"; this.lblSonuc.Visible = true; } catch (Exception ex) { this.lblSonuc.Visible = true; this.lblSonuc.Text = "Hata Oluştu..: (" + ex.Message + ")"; } //*** http://www.aspnetornekleri.com ***// baglanti.Close(); baglanti = null;
mdb dosyasını hostta nereye kaydedeceğimi tarif edebilecek arkadaş var mı?