IT SHARE EDU
Hướng dẫn cách sửa dữ liệu trong # lưu vào datasbase sql server bằng 2 cách khác nhau. Sử dụng các control như combobox, Listview để hiện sản phẩm.
Code c# kết nối với sql server sửa dữ liệu lưu vào data. Ở bài này hướng dẫn sửa tên sản phẩm và giá sản phẩm theo mã sản phẩm.
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 kncsdlhocba
{
public partial class frmSuaDuLieu : Form
{
public frmSuaDuLieu()
{
InitializeComponent();
}
SqlConnection conec = null;
string Strcon = @”Server =DESKTOP-CR8PM8T\SQLEXPRESS; Database=CSDLSANPHAM ; User ID = sa; pwd =123″;
private void frmSuaDuLieu_Load(object sender, EventArgs e)
{
HienLenCobobox();
}
private void HienLenCobobox()
{
if (conec==null)
{
conec = new SqlConnection(Strcon);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = “select * from DanhMuc”;
command.Connection = conec;
SqlDataReader reader = command.ExecuteReader();
cboDanhMuc.Items.Clear();
while (reader.Read())
{
int madm = reader.GetInt32(0);
string TenDm = reader.GetString(1);
cboDanhMuc.Items.Add(madm + “-” + TenDm);
}
reader.Close();
}
int madm = -1;
private void cboDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboDanhMuc.SelectedIndex==-1)
{
return;
}
string line = cboDanhMuc.SelectedItem.ToString();// lấy ra 1 dòng dữ liệu
string[] arr = line.Split(‘-‘); // Tách dâu – ra
madm = int.Parse(arr[0]); // lấy mã vị trí thứ 0
HienThiLenListview(madm);
}
private void HienThiLenListview(int madm)
{
if (conec==null)
{
conec = new SqlConnection(Strcon);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = “select * from SanPham where MaDM=” + madm;
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 btnSua_Click(object sender, EventArgs e)
// sửa dữ liệu không dùng paramater
{
if (conec == null)
{
conec = new SqlConnection(Strcon);
}
if (conec.State == ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = “update SanPham set TenSanPham=N'” + txtTen.Text + “‘, DonGia=” + txtGia.Text + ” where Ma=” + txtMa.Text;
command.Connection = conec;
int n = command.ExecuteNonQuery();
if (n > 0)
{
HienThiLenListview(madm);
MessageBox.Show(” Thêm Thành công”);
}
else
{
MessageBox.Show(” Thêm Thất bại”);
}
}
private void lvSanPham_SelectedIndexChanged(object sender, EventArgs e)
{//hiện thị chi tiết sản phẩm lên texbox
if (lvSanPham.SelectedItems.Count==0)
{
return;
}
ListViewItem lvi = lvSanPham.SelectedItems[0];// lấy 1 dòng dữ liệu nên chỉ chọn 0
txtMa.Text = lvi.SubItems[0].Text;
txtTen.Text = lvi.SubItems[1].Text;
txtGia.Text = lvi.SubItems[2].Text;
}
private void button1_Click(object sender, EventArgs e)
// Sửa dữ liệu bằng cách có sử dụng parameter
{
if (conec ==null)
{
conec = new SqlConnection(Strcon);
}
if (conec.State==ConnectionState.Closed)
{
conec.Open();
}
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = “update SanPham set TenSanPham=@ten, DonGia=@gia where Ma= @ma “;
command.Connection = conec;
command.Parameters.Add(“@ten”, SqlDbType.NVarChar).Value = txtTen.Text;
command.Parameters.Add(“@gia”, SqlDbType.Int).Value = txtGia.Text;
command.Parameters.Add(“@ma”, SqlDbType.Int).Value = txtMa.Text;
int n = command.ExecuteNonQuery();
if (n>0)
{
HienThiLenListview(madm);
MessageBox.Show(” Thêm Sản phẩm Thành Công”);
}
else
{
MessageBox.Show(” Thêm Thất Bại”);
}
}
}
}
video hướng dẫn học lập trình c#