Stored Prosedure Kullanılarak Update İşlemini Gerçekleştirme

Merhaba arkadaşlar, bu makalemizde Stored Procedure kullanarak Employees tablosundaki Ad, Soyada göre personelin Şehir, Ülke sütunundaki bilgilerini güncelleştireceğiz.




Şekil 1


Şekil 2


Şekil 3

Stored Procedure

ALTER PROCEDURE dbo.guncelleEmployees

/*

(

@parameter1 int = 5,

@parameter2 datatype OUTPUT

)

*/

@FirstName varchar(50),

@LastName varchar(50),

@City varchar(50),

@Country varchar(50)

AS

/* SET NOCOUNT ON */

UPDATE Employees SET City = @City, Country = @Country

WHERE FirstName=@FirstName AND LastName=@LastName

RETURN

Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnGuncelle_Click(object sender, EventArgs e)

{

// String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

string str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\northwind.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

SqlConnection baglan = new SqlConnection(str);

SqlCommand komut = new SqlCommand();

komut.CommandType = CommandType.StoredProcedure;

komut.CommandText = "guncelleEmployees";

komut.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtAd.Text.Trim();

komut.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtSoyad.Text.Trim();

komut.Parameters.Add("@City", SqlDbType.VarChar).Value = txtSehir.Text.Trim();

komut.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtUlke.Text.Trim();

komut.Connection = baglan;

try

{

baglan.Open();

komut.ExecuteNonQuery();

Label5.Text = "Kayıt başarılı olarak güncellendi";

}

catch (Exception ex)

{

throw ex;

}

finally

{

baglan.Close();

baglan.Dispose();

}

}

}

Default.aspx

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

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>title>

head>

<body>

<form id="form1" runat="server">

<div>

div>

<asp:Label ID="Label1" runat="server" Text="Ad:">asp:Label>

<asp:TextBox ID="txtAd" runat="server">asp:TextBox>

<br />

<asp:Label ID="Label2" runat="server" Text="Soyad:">asp:Label>

<asp:TextBox ID="txtSoyad" runat="server">asp:TextBox>

<br />

<asp:Label ID="Label3" runat="server" Text="Şehir:">asp:Label>

<asp:TextBox ID="txtSehir" runat="server">asp:TextBox>

<br />

<asp:Label ID="Label4" runat="server" Text="Ülke:">asp:Label>

<asp:TextBox ID="txtUlke" runat="server">asp:TextBox>

<br />

<asp:Label ID="Label5" runat="server" Text="Label">asp:Label>

<br />

<asp:Button ID="btnGuncelle" runat="server" onclick="btnGuncelle_Click"

Text="Güncelle" />

form>

body>

html>

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek dileğiyle. Hoşçakalın. 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...