public bool Insert(TheLoai theLoai) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"INSERT INTO TheLoai(MaLoai,TenLoai) VALUES (@maloai,@tenloai)"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@maloai", theLoai.MaLoai); cmd.Parameters.AddWithValue("@tenloai", theLoai.TenLoai); connection.Open(); int result = cmd.ExecuteNonQuery(); return(result >= 1); } }
public bool UpdateTheLoai(TheLoai theLoai) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"UPDATE TheLoai SET TenLoai = @tenloai WHERE MaLoai = @maloai"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@maloai", theLoai.MaLoai); cmd.Parameters.AddWithValue("@tenloai", theLoai.TenLoai); connection.Open(); int result = cmd.ExecuteNonQuery(); if (result >= 1) { return(true); } } return(false); }
protected void btnSua_Click(object sender, EventArgs e) { if (txtMaLoai.Text != "" && txtTenLoai.Text != "") { TheLoai theLoai = LayDuLieuTuForm(); TheLoaiDAO theloaiDAO = new TheLoaiDAO(); bool result = theloaiDAO.UpdateTheLoai(theLoai); if (result) { lblMessage.Text = "Cập nhật thành công"; LayDuLieuVaoGridView(); } else { lblMessage.Text = "Cập nhật không thành công, vui lòng kiểm tra lại"; } } else { lblMessage.Text = "Vui lòng điền đầy đủ thông tin"; } }
public TheLoai GetTheLoaiByMaLoai(string maloai) { using (SqlConnection connection = new SqlConnection(connectionString)) { string sql = @"SELECT * FROM TheLoai WHERE MaLoai = @maloai"; SqlCommand cmd = new SqlCommand(sql, connection); cmd.Parameters.AddWithValue("@maloai", maloai); connection.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { TheLoai theLoai = new TheLoai { // Lấy giá trị theo tên cột trong CSDL, MaLoai = (string)reader["MaLoai"], TenLoai = (string)reader["TenLoai"] }; return(theLoai); } } return(null); }
private void DoDuLieuLenForm(TheLoai theLoai) { txtMaLoai.Text = theLoai.MaLoai; txtTenLoai.Text = theLoai.TenLoai; }