0

リストに配置する最初の選択があります。リストを使用して各レコードをループし、特定の条件を満たす場所で、一連の挿入、削除、および更新を実行します。最後に、SaveChanges() メソッドを呼び出して変更をコミットします。

コードは例外を発生させずに実行されますが、変更はデータベースに反映されません。私は運が悪いとウェブを検索しています。

SQL2008 バックエンドで VS2008 を使用しています。

助けてください?

            using (SMSEntities db = new SMSEntities())
        {
            try
            {

                //Get SMS's to send from Inbox
                List<Inbox> tmpInbox = (from c in db.Inboxes where c.Status != "NEW" && c.Status != "SUCCESS" select c).ToList();// new { Inbox.InboxID, Inbox.StatusTrackingID, Inbox.Status, Inbox.NoOfAttempts, Inbox.CellNo, Inbox.SourceCellNo, Inbox.Header, Inbox.Message, Inbox.MessageDate, Inbox.AccountID, Inbox.LastAttemptDate }).ToList();

                foreach (Inbox tmpInboxIndex in tmpInbox)
                {
                    bool success = false;

                    //Check status here
                    string SentStatus = CheckSMSSentToProvider(tmpInboxIndex.StatusTrackingID);

                    // Define a transaction scope for the operations.
                    using (TransactionScope transaction = new TransactionScope())
                        {
                        try
                        {
                            if ((SentStatus == "DELIVERED") || (SentStatus == "NOTFOUND") || (SentStatus == "DELETED") || (SentStatus == "REJECTED") || (SentStatus == "UNDELIVERED"))
                            {

                                //Insert the Log row
                                Log newLog = new Log();
                                newLog.InboxID = tmpInboxIndex.InboxID;
                                newLog.CellNo = tmpInboxIndex.CellNo;
                                newLog.SourceCellNo = tmpInboxIndex.SourceCellNo;
                                newLog.Message = tmpInboxIndex.Message;
                                newLog.Header = tmpInboxIndex.Header;
                                newLog.MessageDate = tmpInboxIndex.MessageDate;
                                newLog.AccountID = tmpInboxIndex.AccountID;
                                newLog.ProcessedDate = DateTime.Now;
                                newLog.Status = tmpInboxIndex.Status;
                                newLog.StatusTrackingID = tmpInboxIndex.StatusTrackingID;
                                newLog.NoOfAttempts = tmpInboxIndex.NoOfAttempts;
                                newLog.LastAttemptDate = tmpInboxIndex.LastAttemptDate;
                                db.Logs.AddObject(newLog);

                                //Delete the Inbox row
                                if (tmpInbox != null)
                                {
                                    var deleteInbox = (from c in db.Inboxes where c.InboxID == tmpInboxIndex.InboxID select c).FirstOrDefault();
                                    if (deleteInbox != null)
                                    {
                                        db.DeleteObject(deleteInbox);
                                        //db.SaveChanges(SaveOptions.DetectChangesBeforeSave);
                                    }
                                }
                            }
                            else
                            {
                                //Update inbox status 
                                var tmpUpdateInbox = (from c in db.Inboxes where c.InboxID == tmpInboxIndex.InboxID select c).FirstOrDefault();
                                tmpUpdateInbox.Status = SentStatus;
                                tmpUpdateInbox.NoOfAttempts = tmpInboxIndex.NoOfAttempts + 1;
                                tmpUpdateInbox.LastAttemptDate = DateTime.Now;
                                //db.SaveChanges(SaveOptions.DetectChangesBeforeSave);
                            }
                            // Mark the transaction as complete.
                            transaction.Complete();
                            success = true;
                            //break;
                        }
                        catch (Exception ex)
                        {
                            // Handle errors and deadlocks here and retry if needed.
                            // Allow an UpdateException to pass through and 
                            // retry, otherwise stop the execution.
                            if (ex.GetType() != typeof(UpdateException))
                            {
                                Console.WriteLine("An error occured. "
                                    + "The operation cannot be retried."
                                    + ex.Message);
                                break;
                            }
                            // If we get to this point, the operation will be retried.
                        }
                    }

                    if (success)
                    {
                        // Reset the context since the operation succeeded.
                        //db.AcceptAllChanges();
                        db.SaveChanges();
                    }
                }

                // Dispose the object context.
                db.Dispose();

            }

            catch (Exception exp)
            {
                throw new Exception("ERROR - " + exp.Message.ToString(), exp);
            }
        }
        return true;

よろしく、GPR。

4

2 に答える 2

1

ローカル データベース ファイルを使用していますか? 間違った場所で変更を探している可能性があります。既定では、プログラムの起動時に、VS はデータベース ファイルをデバッグ フォルダーまたはリリース フォルダーにコピーします。次に、プログラムが実行され、デバッグまたはリリース フォルダー内のファイルに変更が加えられて保存されます。プログラムが終了し、ソース フォルダー内のデータベースを見ると、同じように見えます。これを回避するために、絶対パスを使用するように app.config の接続文字列を変更できます。

詳細については、 http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspxを参照してください。

于 2012-03-30T19:58:43.630 に答える
0

SaveChanges への呼び出しを入れないと、TransactionScope は役に立ちません。SaveChanges への呼び出しをそこに移動するか、TransactionScope を完全に削除します。

于 2011-05-18T06:44:07.697 に答える