Hướng dẫn viết stored procedure trong c# và sql server
Hướng dẫn viết stored procedure trong c# và sql server với 2 cách khác nhau
SQL Server biên dịch các stored procedure giúp cho chúng làm việc hiệu quả hơn. Do đó, thay vì tạo các truy vấn động trong mã nguồn của bạn, bạn có thể được lợi ích về việc tái sử dụng và hiệu suất khi sử dụng stored procedure.. Phần sau sẽ cho bạn thấy cách để thay đổi đối tượng SqlCommand để sử dụng stored procedure. Hơn nữa, bạn sẽ thấy một lý do khác tại sao việc hỗ trợ parameter lại là một phần quan trọng của thư viện ADO.NET
Nội dung bài viết này là:
Triệu gọi Store lấy dữ liệu
Triệu gọi Store thêm dữ liệu
Triệu gọi Store thay đổi dữ liệu
Triệu gọi Store
xóa dữ liệu
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 knsdl
{
public partial class frmStore : Form
{
public frmStore()
{
InitializeComponent();
}
SqlConnection conec = null;
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 knsdl
{
public partial class frmStore : Form
{
public frmStore()
{
InitializeComponent();
}
SqlConnection conec = null;
string stronic = @"Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID=sa; pwd =123"
private void frmStore_Load(object sender, EventArgs e)
{
HienThitoanbosanpham();
}
private void HienThitoanbosanpham()
{
if (conec== null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "LayToanBoSanPham";
command.Connection = conec;
SqlDataReader reader = command.ExecuteReader();
lvSanPham.Items.Clear();
while (reader.Read())
{
ListViewItem lvi = new ListViewItem(reader.GetInt32(0) + "");
lvi.SubItems.Add(reader.GetString(1));
lvi.SubItems.Add(reader.GetInt32(2) + "");
lvSanPham.Items.Add(lvi);
}
reader.Close();
}
private void lvSanPham_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvSanPham.SelectedItems.Count==0)
{
return;
}
ListViewItem liv = lvSanPham.SelectedItems[0];
int ma = int.Parse(liv.SubItems[0].Text);
HienThiTheoMaSanPham(ma);
}
private void HienThiTheoMaSanPham(int ma)
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "Hienchitietsanpham";
command.Connection = conec;
SqlParameter parama = new SqlParameter("@ma", SqlDbType.Int);
parama.Value = ma;
command.Parameters.Add(parama);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
txtMa.Text = reader.GetInt32(0) + "";
txtTen.Text = reader.GetString(1);
txtGia.Text = reader.GetInt32(2) + "";
txtMaDM.Text = reader.GetInt32(3) + "";
}
reader.Close();
}
{
HienThitoanbosanpham();
}
private void HienThitoanbosanpham()
{
if (conec== null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "LayToanBoSanPham";
command.Connection = conec;
SqlDataReader reader = command.ExecuteReader();
lvSanPham.Items.Clear();
while (reader.Read())
{
ListViewItem lvi = new ListViewItem(reader.GetInt32(0) + "");
lvi.SubItems.Add(reader.GetString(1));
lvi.SubItems.Add(reader.GetInt32(2) + "");
lvSanPham.Items.Add(lvi);
}
reader.Close();
}
private void lvSanPham_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvSanPham.SelectedItems.Count==0)
{
return;
}
ListViewItem liv = lvSanPham.SelectedItems[0];
int ma = int.Parse(liv.SubItems[0].Text);
HienThiTheoMaSanPham(ma);
}
private void HienThiTheoMaSanPham(int ma)
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "Hienchitietsanpham";
command.Connection = conec;
SqlParameter parama = new SqlParameter("@ma", SqlDbType.Int);
parama.Value = ma;
command.Parameters.Add(parama);
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
txtMa.Text = reader.GetInt32(0) + "";
txtTen.Text = reader.GetString(1);
txtGia.Text = reader.GetInt32(2) + "";
txtMaDM.Text = reader.GetInt32(3) + "";
}
reader.Close();
}
//Hướng dẫn thêm sản phẩm bằng cách viết
StoredProcedure
StoredProcedure
private void btnThemSanPham_Click(object sender, EventArgs e)
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ThemSanPhamMoi";
command.Connection = conec;
command.Parameters.Add("@ma", SqlDbType.Int).Value = txtMa.Text;
command.Parameters.Add("@ten", SqlDbType.NVarChar).Value =txtTen.Text;
command.Parameters.Add("@gia", SqlDbType.Int).Value = txtGia.Text;
command.Parameters.Add("@madm", SqlDbType.Int).Value = txtMaDM.Text;
int n = command.ExecuteNonQuery();
if (n>0)
{
HienThitoanbosanpham();
MessageBox.Show("Thêm Thành Công");
}
else
{
MessageBox.Show("Thêm Thất bại");
}
}
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ThemSanPhamMoi";
command.Connection = conec;
command.Parameters.Add("@ma", SqlDbType.Int).Value = txtMa.Text;
command.Parameters.Add("@ten", SqlDbType.NVarChar).Value =txtTen.Text;
command.Parameters.Add("@gia", SqlDbType.Int).Value = txtGia.Text;
command.Parameters.Add("@madm", SqlDbType.Int).Value = txtMaDM.Text;
int n = command.ExecuteNonQuery();
if (n>0)
{
HienThitoanbosanpham();
MessageBox.Show("Thêm Thành Công");
}
else
{
MessageBox.Show("Thêm Thất bại");
}
}
//Hướng dẫn sửa sản phẩm bằng cách viết
StoredProcedure
StoredProcedure
private void button2_Click(object sender, EventArgs e)
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "CapNhatGiaSanPham";
command.Connection = conec;
command.Parameters.Add("@ma", SqlDbType.Int).Value = txtMa.Text;
command.Parameters.Add("@gia", SqlDbType.Int).Value = txtGia.Text;
int n = command.ExecuteNonQuery();
if (n>0)
{
HienThitoanbosanpham();
MessageBox.Show(" Sửa Thành Công");
}
else
{
MessageBox.Show("Sửa thất bại");
}
}
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "CapNhatGiaSanPham";
command.Connection = conec;
command.Parameters.Add("@ma", SqlDbType.Int).Value = txtMa.Text;
command.Parameters.Add("@gia", SqlDbType.Int).Value = txtGia.Text;
int n = command.ExecuteNonQuery();
if (n>0)
{
HienThitoanbosanpham();
MessageBox.Show(" Sửa Thành Công");
}
else
{
MessageBox.Show("Sửa thất bại");
}
}
//Hướng dẫn xóa sản phẩm bằng cách viết
StoredProcedure
private void btnXoa_Click(object sender, EventArgs e)StoredProcedure
{
if (conec==null)
{
conec = new SqlConnection(stronic);
}
if (conec.State == ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "Xoasanpham";
command.Connection = conec; ;
command.Parameters.Add("@ma", SqlDbType.Int).Value = txtMa.Text;
int n = command.ExecuteNonQuery();
if (n>0)
{
HienThitoanbosanpham();
MessageBox.Show("Xóa Thành Công");
}
}
}
}
Không có nhận xét nào