简体   繁体   English

Linq2SQL不更新SQL Azure上的记录

[英]Linq2SQL not updating record on SQL Azure

I am experimenting with Azure and am running into an issue I cannot seem to find an answer to. 我正在尝试使用Azure,但遇到了一个似乎找不到答案的问题。

I am running a linq query to update Azure Database with changes to a record but for some reason the changes are not being noted or submitted to the database. 我正在运行linq查询,以使用对记录的更改来更新Azure数据库,但是由于某些原因未将更改记入或提交给数据库。 The code I am running is as follows: 我正在运行的代码如下:

DataClasses1DataContext update = new DataClasses1DataContext();

    Company extra = update.Companies.Single(p => p.ID == Convert.ToInt32(Request.QueryString["ID"]));

    extra.companyName = txtCompanyName.Text;
    extra.address1 = txtAddress1.Text;
    extra.address2 = txtAddress2.Text;
    extra.address3 = txtAddress3.Text;
    extra.address4 = txtAddress4.Text;
    extra.town = txtTown.Text;
    extra.county = txtCounty.Text;
    extra.postCode = txtPostCode.Text;
    extra.billingEmail = txtBillingEmail.Text;
    extra.facebook = txtFacebook.Text;
    extra.fax = txtFaxNumber.Text;
    extra.faxArea = txtFaxArea.Text;
    extra.faxCountry = txtFaxCountry.Text;
    extra.linkedIn = txtLinkedIn.Text;
    extra.mainEmail = txtMainEmail.Text;
    extra.mainPhone = txtMainLineNumber.Text;
    extra.mainPhoneArea = txtMainLineArea.Text;
    extra.mainPhoneCountry = txtMainLineCountry.Text;

    if (drpMarketing.Text == "No")
    {
        extra.marketingYesNo = 0;
    }
    else
    {
        extra.marketingYesNo = 1;
    }

    extra.salesEmail = txtSalesEmail.Text;
    extra.twitter = txtTwitter.Text;
    extra.website = txtWebsite.Text;

    update.SubmitChanges();

What I am noticing by setting break points is that if I load the page and then change some data then the new data is not being reflected when this code is called (it is in a btnUpdate_Click event) 通过设置断点,我注意到的是,如果我加载页面然后更改一些数据,则在调用此代码时(在btnUpdate_Click事件中)新数据不会得到反映。

So When the submitChanges() is used then the new data is not present to submit. 因此,当使用submitChanges()时,将不存在要提交的新数据。 Any help appreciated.... I think I have looked at this for so long it needs some fresh eyes lol. 任何帮助表示赞赏。。。我想我已经看了很久了,所以需要一些新鲜的眼睛。

Are you sure you are not repopulating your form OnPostback with the original data (re-selected from the database) so that the linq update actually works, but you are always submitting the most recent database data back to the database. 您确定不使用原始数据(从数据库中重新选择)重新填充表单OnPostback,以使linq更新确实有效,但是您始终将最新的数据库数据提交回数据库。

In other words are you sure you are only populating your form 换句话说,您确定只填写表格

if(!Page.IsPostback)
{
//populate the form
}

this will preserve user amends when a postback happens. 回发发生时,这将保留用户的修改。

Have you tried to wrap the SubmitChanges in a try block? 您是否尝试过在try块中包装SubmitChanges? Maybe it is throwing an exception. 也许它引发了异常。 Here is how it is documented to do updates which is very similar to what you are doing: 这是进行更新的文档记录,该更新与您正在执行的操作非常相似:

http://msdn.microsoft.com/en-us/library/bb399339.aspx http://msdn.microsoft.com/zh-CN/library/bb399339.aspx

Also, does this work against a local SQL Server database or SQL Express database? 此外,这是否适用于本地SQL Server数据库或SQL Express数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM