SQL Server insert Trigger örneği için tıklayınız.
c# KODLARI:
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 System.Data.SqlClient;
namespace trigger
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(“Data Source=.\\SQLEXPRESS; Initial Catalog=market; Integrated Security=true;”);
private void Form1_Load(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sorgu = “select urunAdi from urunler”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu,conn);
DataSet ds = new DataSet();
adp.Fill(ds);
for (int i = 0; i <= ds.Tables[0].Rows.Count – 1; i++)
comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
private void button1_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sorgu = “select urunid from urunler where urunAdi='”+comboBox1.Text+”‘”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu, conn);
DataSet ds = new DataSet();
adp.Fill(ds);
int urunNo = Convert.ToInt16(ds.Tables[0].Rows[0][0].ToString());
string ekleme_sorgusu = “insert into satislar(urunid,satisAdedi) values (“+urunNo+”,”+textBox1.Text+”)”;
SqlCommand cmd = new SqlCommand(ekleme_sorgusu,conn);
cmd.ExecuteNonQuery();
MessageBox.Show(“Giriş Yapılmıştır…”);
}
}
}
SQL Server Trigger Kısmı:
Create trigger [dbo].[urunAdediGuncelle] on [dbo].[satislar]
for insert
as begin
declare @urunid int, @satisadedi int
select @urunid =urunid, @satisAdedi=satisAdedi from inserted
update urunler set urunadedi=urunadedi-@satisadedi where urunid=@urunid
end
2 Comments
Comments are closed.