网页更新数据库,但如果我没有写任何东西,他会删除数据库中的数据,如何检查代码中是否有更新
protected void btnsave_Click(object sender, EventArgs e)
{
using (DataClasses1DataContext sdc = new DataClasses1DataContext()) {
string fileName = FileUpload1.FileName;
byte[] fileByte = FileUpload1.FileBytes;
Binary binaryObj = new Binary(fileByte);
Professor_Dim prof = sdc.Professor_Dims.SingleOrDefault(x => x.P_ID == 0);
prof.P_Fname = txtfirstname.Text;
prof.P_Lname = txtlastname.Text;
prof.P_Email = txtemail.Text;
prof.P_Address = txtaddress.Text;
prof.P_Phone = txtphone.Text;
prof.P_Image = binaryObj;
sdc.SubmitChanges();
}
}
答案 0 :(得分:2)
protected void btnsave_Click(object sender, EventArgs e)
{
using (DataClasses1DataContext sdc = new DataClasses1DataContext())
{
string fileName = FileUpload1.FileName;
byte[] fileByte = FileUpload1.FileBytes;
Binary binaryObj = new Binary(fileByte);
Professor_Dim prof = sdc.Professor_Dims.SingleOrDefault(x => x.P_ID == 0);
if ( !string.IsNullOrEmpty(txtfirstname.Text))
prof.P_Fname = txtfirstname.Text;
if ( !string.IsNullOrEmpty(txtlastname.Text))
prof.P_Lname = txtlastname.Text;
if ( !string.IsNullOrEmpty(txtemail.Text))
prof.P_Email = txtemail.Text;
if ( !string.IsNullOrEmpty(txtaddress.Text))
prof.P_Address = txtaddress.Text;
if ( !string.IsNullOrEmpty(txtphone.Text))
prof.P_Phone = txtphone.Text;
prof.P_Image = binaryObj;
sdc.SubmitChanges();
}
}
答案 1 :(得分:1)
您想使用GetChangeSet()
ChangeSet cs = sdc.GetChangeSet();
https://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getchangeset(v=vs.110).aspx
这将为您提供一个包含三个成员的对象,一个已更改行的列表,一个已删除行列表以及一个已插入行列表。