1

我正在尝试从表中获取记录列表,然后在foreach循环中我试图update逐个记录记录。

这是我的代码

     public IEnumerable<MessageOut> GetSMSInformation()
        {
            using (var db = new DataClasses1DataContext())
            {
                var sms = db.MessageOuts.Where(msg => msg.msgstatus.Equals("Pending")).Select(msg => msg);
                return sms.ToList();
            }
        }



     public void StartSMSSending()
        {
            var information = GetSMSInformation();

            foreach (var sms in information)
            {
                SendSMS(sms.reciever, sms.msg);
                UpdateRecords(sms,"Delivered", DateTime.Now);
            }
        }

   public void UpdateRecords(MessageOut sms, string msgStatus, DateTime sentTime)
        {
            using (var db = new DataClasses1DataContext())
            {
                sms.msgstatus = msgStatus;
                sms.senttime = sentTime;
                db.SubmitChanges();
            }
        }

记录只是不更新​​。我也没有收到任何错误。

有人可以帮我解决这个问题吗?

4

3 回答 3

7

为了使用 Linq2Sql 类执行更新操作,您需要保持上下文处于活动状态。在上面的示例中,您将您的选择包含在 using 语句中,因此您的记录被返回,然后上下文被转储。当您进行更新时,会有一个新的上下文,因此没有注册到对象的更改。一种方法是使上下文成为更全局的对象或将其作为参数传递。

public IEnumerable<MessageOut> GetSMSInformation(DataContext context)
{
    var sms = context.MessageOuts.Where(
        msg => msg.msgstatus.Equals("Pending")).Select(msg => msg);
    return sms.ToList();
}

public void StartSMSSending()
{
    using (var db = new DataClasses1DataContext())
    {
        var information = GetSMSInformation(db);

        foreach (var sms in information)
        {
            SendSMS(sms.reciever, sms.msg);
            UpdateRecords(sms,"Delivered", DateTime.Now, db);
        }
    }
}

public void UpdateRecords(MessageOut sms, string msgStatus, DateTime sentTime, DataContext context)
{
    sms.msgstatus = msgStatus;
    sms.senttime = sentTime;
    context.SubmitChanges();
}

或者,您可以这样做(我的首选方法):

public IEnumerable<MessageOut> GetSMSInformation()
{
    var sms = Context.MessageOuts.Where(
        msg => msg.msgstatus.Equals("Pending")).Select(msg => msg);
    return sms.ToList();
}

public void StartSMSSending()
{
    var information = GetSMSInformation(db);

    foreach (var sms in information)
    {
        SendSMS(sms.reciever, sms.msg);
        UpdateRecords(sms,"Delivered", DateTime.Now, db);
    }
}

public void UpdateRecords(MessageOut sms, string msgStatus, DateTime sentTime)
{
    sms.msgstatus = msgStatus;
    sms.senttime = sentTime;
    Context.SubmitChanges();
}

private DataClasses1DataContext _context = null;
public DataClasses1DataContextContext
{
    get
    {
        return _context ?? (_context = new DataClasses1DataContext());
    }
}
于 2012-10-09T11:47:05.640 回答
3

您正在从此数据库上下文中提取数据:

    public IEnumerable<MessageOut> GetSMSInformation()
    {
        using (var db = new DataClasses1DataContext())
        {
            var sms = db.MessageOuts.Where(msg => msg.msgstatus.Equals("Pending")).Select(msg => msg);
            return sms.ToList();
        }
    }

然后,您正在从先前的数据库上下文更新数据,您是否正在将更改提交到不同的数据库上下文:

    public void UpdateRecords(MessageOut sms, string msgStatus, DateTime sentTime)
    {
        using (var db = new DataClasses1DataContext())
        {
            sms.msgstatus = msgStatus;
            sms.senttime = sentTime;
            db.SubmitChanges();
        }
    }

将数据库上下文作为参考传递,或者最好将其作为类的成员。这确保了您的类实例的单个数据库上下文实例。

我的建议如下:

class SmsCenter
{
        private DataClasses1DataContext _dbContext;

        public SmsCenter()
        {
            _dbContext = new DataClasses1DataContext();
        }

        public IEnumerable<MessageOut> GetSMSInformation()
        {
            var sms = _dbContext.MessageOuts.Where(msg => msg.msgstatus.Equals("Pending")).Select(msg => msg);
            return sms.ToList();
        }



        public void StartSMSSending()
        {
            var information = GetSMSInformation();

            foreach (var sms in information)
            {
                SendSMS(sms.reciever, sms.msg);
                UpdateRecords(sms,"Delivered", DateTime.Now);
            }
        }

        public void UpdateRecords(MessageOut sms, string msgStatus, DateTime sentTime)
        {
            sms.msgstatus = msgStatus;
            sms.senttime = sentTime;
            _dbContext.SubmitChanges();
        }
}
于 2012-10-09T11:46:48.783 回答
1

您不会将“短信”对象与数据库相关联,例如

            sms.msgstatus = msgStatus;
            sms.senttime = sentTime;
            db.MessageOut.Add(sms);
            //or
            db.MessageOut=sms;
            db.SubmitChanges();
于 2012-10-09T12:01:07.927 回答