C# 在不同页面上使用数据访问层调用方法不会更新数据库
我有一个数据访问层要更新,试图调用不同页面上的方法不会更新数据库 我尝试将一些代码移动到循环之外,并设置了一个断点,但数据没有到达断点 数据访问层代码:C# 在不同页面上使用数据访问层调用方法不会更新数据库,c#,database,data-access-layer,C#,Database,Data Access Layer,我有一个数据访问层要更新,试图调用不同页面上的方法不会更新数据库 我尝试将一些代码移动到循环之外,并设置了一个断点,但数据没有到达断点 数据访问层代码: public void editBuyerByID(int bID, string title, string fname, string lname, string email, string pWord, string tel,string pCode, double price, string type) { using (var c
public void editBuyerByID(int bID, string title, string fname, string lname, string email, string pWord, string tel,string pCode, double price, string type)
{
using (var context = dc)
{
Buyer editBuyer = (from b in context.Buyers where b.BuyerID == bID select b).FirstOrDefault();
if (editBuyer != null)
{
editBuyer.BuyerTitle = title;
editBuyer.BuyerFName = fname;
editBuyer.BuyerLName = lname;
editBuyer.BuyerEmail = email;
editBuyer.BuyerPassword = pWord;
editBuyer.BuyerTelNo = tel;
editBuyer.BuyerPostcode = pCode;
editBuyer.BuyerPriceRange = price;
editBuyer.BuyerType = type;
//context.Buyers.Add(editBuyer);
context.SaveChanges();
}
}
}
调用方法的帐户页:
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
aDal.editBuyerByID(Convert.ToInt32(Session["Buyer"].ToString()),
txtTitle.Text, txtFName.Text, txtLName.Text,
txtEmail.Text, txtPassword.Text, txtTelNo.Text, txtPostcode.Text, Convert.ToSingle(txtPrice.Text), txtType.Text);
string editBuyerScript = "alert(\"Your details have been updated\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", editBuyerScript, true);
}
catch
{
string invalidScript = "alert(\"All fields must be filled correctly\");";
ScriptManager.RegisterStartupScript(this, GetType(),
"ServerControlScript", invalidScript, true);
}
}
在DAL中,editBuyerByID方法可以接收更改的买方对象,而不是一组字段,在该方法中,只需将实体标记为已更改并保存更改:
public void editBuyerByID(Buyer buyer)
{
using (var context = dc)
{
context.Entry(buyer).State = EntityState.Modified;
context.SaveChanges();
}
}
在btnUpdate_Click事件中,进行以下更改,获取买方对象(假设您在DAL中有通过ID获取买方的方法),更新字段并发送到editBuyerByID DAL方法:
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
int buyerId = Convert.ToInt32(Session["Buyer"].ToString());
Buyer buyer = aDal.getBuyerByID(buyerId);
if(buyer!=null)
{
buyer.BuyerTitle = txtTitle.Text;
buyer.BuyerFName = txtFName.Text;
buyer.BuyerLName = txtLName.Text;
buyer.BuyerEmail = txtEmail.Text;
buyer.BuyerPassword = txtPassword.Text;
buyer.BuyerTelNo = txtTelNo.Text;
buyer.BuyerPostcode = txtPostcode.Text;
buyer.BuyerPriceRange = Convert.ToSingle(txtPrice.Text);
buyer.BuyerType = txtType.Text;
aDal.editBuyerByID(buyer);
}
string editBuyerScript = "alert(\"Your details have been updated\");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", editBuyerScript, true);
}
catch
{
string invalidScript = "alert(\"All fields must be filled correctly\");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", invalidScript, true);
}
}
由于您使用的是面向对象编程语言C,因此可以使用买方类对象而不是一长串字段将买方详细信息传递给EditBuyerByd方法!我该怎么做呢?在这方面的任何帮助都会很感激你有没有遇到任何错误,如果是,错误消息是什么?不,只是它没有在数据库中更新消息出现,更新成功,但检查后,它没有获得买家id的方法,只有一个买家列表我在DAL中有此代码,我不确定这是否与您的意思相同:公共买家FindBuyerBydint id{使用var context=new DBModel{Buyer findBuyer=from b in context.BuyerID==ID的买家选择b.FirstOrDefault;return findBuyer;}}}任何帮助都将不胜感激