我正在做一个项目。该项目涉及一个数据库,其中包含一个名为“Customers”的表。在同一个项目中,我有一个名为“frmAddNewCustomer”的表单。此表单包含与客户相关的 9 个属性。有一个按钮,单击该按钮时会包含允许将这些属性分别输入数据库的代码。
我还有一个名为“CustomerDAL”的类,它允许我在数据库表上执行任务(插入、更新、删除等),并且该类包含我用来从“frmAddNewCustomer”表单向数据库输入数据的方法。
最后,我有一个名为“CustomerModel”的类,它代表数据库中的一条记录。在 CustomerDAL 类中,前面提到的方法(允许我通过 UI 表单向数据库输入数据的方法)的参数是从 CustomerModel 类创建的对象。我的问题是,在 UI 表单中,它表示该方法没有 9 个参数的重载。
这是客户模型类:
public int CustomerID { get; set; }
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string Streetname { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
public string WeddingGiftList { get; set; }
public CustomerModel( string title, string forename, string surname, string email, string streetname,string town, string county, string postcode, string weddingGiftlist)
{
Title = title;
Forename = forename;
Surname = surname;
Email = email;
Streetname = streetname;
Town = town;
County = county;
Postcode = postcode;
WeddingGiftList = weddingGiftlist;
}
public CustomerModel()
{
}
这是 CustomerDAL 类:
private static string _connectionString = ConfigurationManager.ConnectionStrings["SimpsonsConnection"].ConnectionString;
public static int AddNewCustomer(CustomerModel newCustomer)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
string sqlQuery = string.Format("INSERT INTO [Customer] VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}','{6}','{7}','{8}')", newCustomer.Title, newCustomer.Forename, newCustomer.Surname, newCustomer.Email, newCustomer.Streetname, newCustomer.Town, newCustomer.County, newCustomer.Postcode, newCustomer.WeddingGiftList);
SqlCommand insertCommand = new SqlCommand(sqlQuery, connection);
int rowsAffected = insertCommand.ExecuteNonQuery();
connection.Close();
return rowsAffected;
}
}
//this is the UI forms click event on the button:
//this is to add the customer details to the database when the 'Create button is clicked'
if ( cmbxTitle.Text != "" || txtForename.Text != "" || txtSurname.Text != "" || txtEmail.Text != "" || txtStreetName.Text != "" || txtTown.Text != "" || txtCounty.Text != "" || txtPostCode.Text != "" || cmbxWeddingGiftList.Text != "")
{
int rowsAffected = CustomerDAL.AddNewCustomer(cmbxTitle.Text, txtForename.Text, txtSurname.Text, txtEmail.Text, txtStreetName.Text, txtTown.Text, txtCounty.Text, txtPostCode.Text, cmbxWeddingGiftList.Text);
if(rowsAffected == 1)
{
MessageBox.Show("Customer has been added successfully");
Form myNextScreen = new frmMenu();
myNextScreen.Show();
}
else
{
MessageBox.Show("Customer was not able to be registered. Please re-enter details carefully");
}
}
else
{
lblInfo.Visible = true;
MessageBox.Show("Please enter all details");
}
在 UI 表单中,我的错误是当我从 CustomerDAL 类中引用“AddNewCustomer”方法时。
我只是不确定如何解决这个错误,因为我认为我有 9 个参数? 如果你能帮我解决这个问题,那将意义重大,因为我对 C# 中的数据库还比较陌生
在我看来,你的问题可能在于没有定义对象
int rowsAffected = CustomerDAL.AddNewCustomer(new CustomerModel{....});
而不是这个
int rowsAffected = CustomerDAL.AddNewCustomer(cmbxTitle.Text, txtForename.Text, txtSurname.Text, txtEmail.Text, txtStreetName.Text, txtTown.Text, txtCounty.Text, txtPostCode.Text, cmbxWeddingGiftList.Text);
有这个
var newCustomer = new Customer
{
Title = cmbxTitle.Text,
//rest of customer properties
};
int rowsAffected = CustomerDAL.AddNewCustomer(newCustomer)