linq查询中出现未处理的异常

本文关键字:未处理 异常 查询 linq | 更新日期: 2023-09-27 18:16:22

描述:由于出现未处理的异常,进程已终止。异常信息:处的System.InvalidOperationException堆栈System.Data.Linq.Table 1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].CheckReadOnly()
at System.Data.Linq.Table
1[[System._Canon,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]。DeleteOnSubmit(System._Canon(位于位于的ReportSender.EmailReportApp.Execute((位于的System.Threading.ThreadHelper.ThreadStart_Context(System.Object(System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.Context回调,System.Object,布尔值(System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.Context回调,System.Object(System.Threading.ThreadHelper.ThreadStart((

在尝试删除以下查询中的记录时,我出现了上述错误:这个错误到底意味着什么?我该如何修复它?

private void Execute()
{
    try
    {
        // Check for a new record
        DataClasses1DataContext dc = new DataClasses1DataContext();
        foreach (var item in dc.reportsSent1s)
        {
            string matchedCaseNumber = item.CaseNumberKey;
            (new MyReportRenderer()).RenderTest(matchedCaseNumber);
            dc.reportsSent1s.DeleteOnSubmit(item);
            dc.SubmitChanges();
        }   
    }
    catch (ThreadAbortException ex)
    {
        _log.WriteEntry(ex.StackTrace.ToString());
    }
}

其余代码如下:

public class MyReportRenderer
{
    private rs2005.ReportingService2005 rs;
    private rs2005Execution.ReportExecutionService rsExec;
    public void RenderTest(String matchedCaseNumber)
    {
        string HistoryID = null;
        string deviceInfo = null;
        string encoding = String.Empty;
        string mimeType = String.Empty;
        string extension = String.Empty;
        rs2005Execution.Warning[] warnings = null;
        string[] streamIDs = null;
        rs = new rs2005.ReportingService2005();
        rsExec = new rs2005Execution.ReportExecutionService();
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
        rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
        rs.Url = "http://***.**.***.**/ReportServer_DEVELOPMENT/ReportService2005.asmx";
        rsExec.Url = "http://***.**.***.**/ReportServer_DEVELOPMENT/ReportExecution2005.asmx";
        try
        {
            // Load the selected report.
            rsExec.LoadReport("/LawDept/LawDeptTIC", HistoryID);
            // Set the parameters for the report needed.
            rs2005Execution.ParameterValue[] parameters = new rs2005Execution.ParameterValue[1];
            parameters[0] = new rs2005Execution.ParameterValue();
            parameters[0].Name = "CaseNumberKey";
            parameters[0].Value = matchedCaseNumber;
            rsExec.SetExecutionParameters(parameters, "en-us");
            // get pdf of report 
            Byte[] results = rsExec.Render("PDF", deviceInfo,
            out extension, out encoding,
            out mimeType, out warnings, out streamIDs);
            //pass paramaters for email
            DataClasses1DataContext db = new DataClasses1DataContext();
            var matchedBRT = (from c in db.GetTable<vw_ProductClientInfo>()
                              where c.CaseNumberKey == matchedCaseNumber
                              select c.BRTNumber).SingleOrDefault();
            var matchedAdd = (from c in db.GetTable<vw_ProductClientInfo>()
                              where c.CaseNumberKey == matchedCaseNumber
                              select c.Premises).SingleOrDefault();
            //send email with attachment
            MailMessage message = new MailMessage("Rts@acmets.com", "gge.grer@gmail.com", "Report for BRT # " + matchedAdd, "Attached if the Tax Information Certificate for the aboved captioned BRT Number");
            MailAddress copy = new MailAddress("cts@gmail.com");
            message.CC.Add(copy);
            SmtpClient emailClient = new SmtpClient("***.**.***.**");
            message.Attachments.Add(new Attachment(new MemoryStream(results), String.Format("{0}" + matchedBRT + ".pdf", "BRT")));
            emailClient.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

linq查询中出现未处理的异常

看起来LINQ to SQL正在检查该表是否为只读表,并判断该表为只读表并因此抛出(不能从只读表中删除(。因此,您需要研究为什么LINQ to SQL认为该表是只读的。你桌上有主键吗?我知道,没有表是LINQ to SQL认为表是只读的常见原因(LINQ to SQL需要它作为其标识映射(。如果这不起作用,George Johnston建议尝试将调用转移到循环之外的SubmitChanges,这是一个很好的建议;在表上获取枚举器可能会激活上下文中的只读锁,这就是CheckReadOnly在调用提交时失败的原因。