//我有一个 KhachHang.cs 文件,其中包含代码:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuanLyKhachHangWinApp.Class
{
class KhachHang //Customer
{
private int _MaKH; //ID
private string _TenKH; //Name
private string _GioiTinh; //Sex
private string _SDT; //Phone
private DateTime _NgaySinh; //DOB
private string _LoaiKH; //Type of Customer
public KhachHang()
{
}
public KhachHang(int maKH, string tenKH, string gioiTinh, string sDT, DateTime ngaySinh, string loaiKH)
{
_MaKH = maKH;
_TenKH = tenKH;
_GioiTinh = gioiTinh;
_SDT = sDT;
_NgaySinh = ngaySinh;
_LoaiKH = loaiKH;
}
public int MaKH { get => _MaKH; set => _MaKH = value; }
public string TenKH { get => _TenKH; set => _TenKH = value; }
public string GioiTinh { get => _GioiTinh; set => _GioiTinh = value; }
public string SDT { get => _SDT; set => _SDT = value; }
public DateTime NgaySinh { get => _NgaySinh; set => _NgaySinh = value; }
public string LoaiKH { get => _LoaiKH; set => _LoaiKH = value; }
public void ThemKhachHang(KhachHang khachHang)
{
SqlConnection sqlConnection = Connection.GetSqlConnection();
SqlCommand cmd = new SqlCommand("ThemKhachHang", sqlConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@MaKH", khachHang.MaKH));
cmd.Parameters.Add(new SqlParameter("@TenKH", khachHang.TenKH));
cmd.Parameters.Add(new SqlParameter("@GioiTinh", khachHang.GioiTinh));
cmd.Parameters.Add(new SqlParameter("@SDT", khachHang.SDT));
cmd.Parameters.Add(new SqlParameter("@NgaySinh", khachHang.NgaySinh));
cmd.Parameters.Add(new SqlParameter("@LoaiKH", khachHang.LoaiKH));
sqlConnection.Open();
cmd.ExecuteNonQuery();
sqlConnection.Close();
}
}
}
/并且 SQL Server 中 Customer 的这些属性的类型具有数据类型,我确保 SQL Server 与 C# 连接(因为 SQL 的默认数据已更新到我的主窗体) 创建表 Khachhang ( MaKH int 主键, TenKH nvarchar(50), GioiTinh char(3) 检查(GoiTinh in ('Nam','Nu')), SDT 字符(20), NgaySinh 日期, LoaiKH char(10) 检查(LoaiKH in ('VIP','Thuong')) ) 去/
//然后我就有了一个frmAdd.cs文件
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 QuanLyKhachHangWinApp.Class;
namespace QuanLyKhachHangWinApp
{
public partial class frmAdd : Form
{
public frmAdd()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void frmAdd_Load(object sender, EventArgs e)
{
}
private void bntAdd(object sender, EventArgs e)
{
ThemKhachHang();
this.Close();
}
void ThemKhachHang()
{
KhachHang khachHang = new KhachHang();
// Điền thông tin cho khachHang từ các trường nhập liệu trên form
khachHang.MaKH = int.Parse(txtIDNew.Text);
khachHang.TenKH = txtNameNew.Text;
khachHang.GioiTinh = cboSexNew.SelectedItem.ToString();
khachHang.SDT = txtPhoneNew.Text;
khachHang.NgaySinh = DateTime.Parse(dtmDateAdd.Text);
khachHang.LoaiKH = cboTypeCustomerNew.SelectedItem.ToString();
khachHang.ThemKhachHang(khachHang);
}
}
}
//我的代码没有抛出任何错误,但它没有像我想要的那样工作
我运行代码,它将出现 frmAdd.cs (它确实出现了),但是当我从键盘将数据输入到表单中的字段并按“添加”按钮时,“添加”按钮不起作用(它没有添加我的数据并关闭表单)。我希望数据将添加到 SQL Server 中并关闭表单。
因为我的代码没有抛出任何错误,所以我不知道如何修复它(我设置或传入的数据类型是否有不足?)
尝试在 SQL Management Studio 中调用相同的存储过程并检查它是否正常工作