Chào bạn thamlang24! Chân thành cảm ơn bạn đã góp ý để cho đoạn mã này được hoàn thiện. Lý do bạn không insert tiếng Việt được là do cú pháp của sql cho Sql Server là bạn cần thêm chữ N trước chuỗi tiếng Việt. Bạn chỉ cần thêm chữ N ở đoạn mã sau
strInsertCTHD = "N'" tmp.TenSP "',";
Như vậy bạn chỉ cần sửa lại một chút trong method Insert. Bạn có thể copy đoạn mã bên dưới để paste vào mã nguồn và lúc này sẽ insert được dữ liệu tiếng Việt.
public bool Insert()
{
string strInsertHD = "insert into HOADON(MaKH,NgayDat) Values(";
strInsertHD = Order.MaKH ",";
strInsertHD = "'" Order.NgayDat "')";
int result = 0;
if (_conn.State == ConnectionState.Closed)
if (!this.Connect())
return false;
SqlCommand command = new SqlCommand(strInsertHD, _conn);
try
{
result = command.ExecuteNonQuery();
if (result > 0)
{
string mahd = this.GetMaHD().ToString();
foreach(OrderDetail tmp in Order.OrderDetails)
{
string strInsertCTHD = "insert into CTHOADON(MaHD,TenSP,SoLuong,DonGia) Values(";
strInsertCTHD = mahd ",";
strInsertCTHD = "N'" tmp.TenSP "',";
strInsertCTHD = tmp.SoLuong.ToString() ",";
strInsertCTHD = tmp.DonGia.ToString() ")";
command = new SqlCommand(strInsertCTHD, _conn);
result = command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
_error = ex.Message;
}
if (result <= 0)
return false;
return true;
}
Một lần nữa cảm ơn bạn. Chúc bạn thành công!