Sql Veritabanı Insert, Update İşlemleri

Merhaba arkadaşlar bu makalemizde Sql veritabanımıza yeni kayıt girişi yapacağız. Veritabanındaki mevcut kayıtlarda güncelleme işlemi gerçekleştireceğiz.

Screenshot


Şekil 1


Şekil 2
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataBind();
        }
    }
    protected void dataBind()
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True;Connect Timeout=30");
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        cmd.CommandText = "Select * From Personel";

        cmd.Connection = con;
        con.Open();

        cmd.ExecuteNonQuery();

        da.Fill(ds);

        con.Close();

        GridView1.DataSource = ds;

        GridView1.DataBind();

    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        string insertSQL;
        insertSQL = "INSERT INTO Personel (";
        insertSQL += "Id, Ad, Soyad,  ";
        insertSQL += "Kısım, Mail) ";
        insertSQL += "VALUES (";
        insertSQL += "@id, @fname, @lname, ";
        insertSQL += "@department, @mail)";
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True;Connect Timeout=30");
        SqlCommand cmd = new SqlCommand(insertSQL, con);
      
        // Add the parameters. Parametreleri ekle.
        cmd.Parameters.AddWithValue("@id", txtId.Text);
        cmd.Parameters.AddWithValue("@fname", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@lname", txtLastName.Text);
        cmd.Parameters.AddWithValue("@department", txtDepartment.Text);
        cmd.Parameters.AddWithValue("@mail", txtMail.Text);
       
      
       
        int i = 0;
        try
        {
            con.Open();
            i = cmd.ExecuteNonQuery();
            lblStatus.Text = i.ToString() + " record inserted... girilen kayit kaydedildi.";
            dataBind();
        }
        catch (Exception err)
        {
            lblStatus.Text = "Error inserting record... kayit girisi hatasi. ";
            lblStatus.Text += err.Message;
        }
        finally
        {
            con.Close();
        }

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string updateSQL;
        updateSQL = "UPDATE Personel SET ";
        updateSQL += "Ad=@fname, Soyad=@lname, ";
        updateSQL += "Kısım=@department, Mail=@mail ";
        updateSQL += "WHERE id=@id";
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True;Connect Timeout=30");
        SqlCommand cmd = new SqlCommand(updateSQL, con);
      
        // Add the parameters. parametreleri ekle.
        cmd.Parameters.AddWithValue("@id", txtId.Text);
        cmd.Parameters.AddWithValue("@fname", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@lname", txtLastName.Text);
        cmd.Parameters.AddWithValue("@department", txtDepartment.Text);
        cmd.Parameters.AddWithValue("@mail", txtMail.Text);
       
      
        int i = 0;
        try
        {
            con.Open();
            i = cmd.ExecuteNonQuery();
            lblStatus.Text = i.ToString() + " record updated... kayit guncellendi.";
            dataBind();
        }
        catch (Exception err)
        {
            lblStatus.Text = "Error updating record... kayit guncelleme hatasi.  ";
            lblStatus.Text += err.Message;
        }
        finally
        {
            con.Close();
        }

    }
}
  
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label5" runat="server" Text="Id :"></asp:Label>
                      
        <asp:TextBox ID="txtId" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="First Name :"></asp:Label>
        
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
   
        <asp:Label ID="Label2" runat="server" Text="Last Name :"></asp:Label>
 <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="Department :"></asp:Label>
       
        <asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox>
   
        <asp:Label ID="Label4" runat="server" Text="Mail :"></asp:Label>
          
        <asp:TextBox ID="txtMail" runat="server"></asp:TextBox>
        <br />
        <br />
                          
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" Width="141px" />
   
        <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" Width="151px" />
        <br />
        <br />
    
        <asp:Label ID="lblStatus" runat="server" Text="Status"></asp:Label>
        <br />
 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="519px">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...