使用linq和DataSet更新两个字段

时间:2015-11-10 16:37:03

标签: c# linq

我需要更新数据库中的两个字段。这些是规则: 1)如果这些字段(DT_GERACAO和BL_RELATORIO)为空,那么我更新。

好吧,我的问题是我的应用程序是一个控制台应用程序。我没有上下文,所以我没有使用linq或lambda表达式。

请参阅下面的完整代码。

public static void Emitir()
        {
            //Relatório com DataSource = ORACLE
            dsPlanoMedico.PLANO_MEDICODataTable dtPlanoMedico = new dsPlanoMedico.PLANO_MEDICODataTable();
            dsPlanoMedico.POC_SOLIC_RELATORIODataTable dtSolicRel = new dsPlanoMedico.POC_SOLIC_RELATORIODataTable();

            dsPlanoMedicoTableAdapters.PLANO_MEDICOTableAdapter adapt = new dsPlanoMedicoTableAdapters.PLANO_MEDICOTableAdapter();
            dsPlanoMedicoTableAdapters.POC_SOLIC_RELATORIOTableAdapter solic_adapt = new dsPlanoMedicoTableAdapters.POC_SOLIC_RELATORIOTableAdapter();

            adapt.Fill(dtPlanoMedico);
            solic_adapt.Fill(dtSolicRel);
            //dtPlanoMedico.Where(i => i.IND_REGULAMENTADO == "S");

            var dv = new System.Data.DataView(dtPlanoMedico);
            dv.RowFilter = "IND_REGULAMENTADO LIKE 'N' and TIPO_REGISTRO_ANS LIKE 'D'";
            //dv.RowFilter = "TIPO_REGISTRO_ANS LIKE 'D'";

            ReportDataSource rds = new ReportDataSource("dsDados", dv);
            ReportViewer viewer = new ReportViewer();

            viewer.ProcessingMode = ProcessingMode.Local;
            viewer.LocalReport.ReportPath = "ReportBD.rdlc";
            //viewer.LocalReport.SetParameters(new ReportParameter("Regulamentado", "S"));
            viewer.LocalReport.DataSources.Add(rds);

            Warning[] warnings;
            string[] streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;

            byte[] bytesPDF = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fsPDF = new FileStream("c:\\Relatemp\\report.pdf", FileMode.Create);
            fsPDF.Write(bytesPDF, 0, bytesPDF.Length);
            fsPDF.Close();
            fsPDF.Dispose();

            byte[] bytesExcel = viewer.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fsExcel = new FileStream("c:\\Relatemp\\report.xls", FileMode.Create);
            fsExcel.Write(bytesExcel, 0, bytesExcel.Length);
            fsExcel.Close();
            fsExcel.Dispose();

            byte[] bytesWord = viewer.LocalReport.Render("Word", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
            FileStream fsWord = new FileStream("c:\\Relatemp\\report.doc", FileMode.Create);
            fsWord.Write(bytesWord, 0, bytesWord.Length);
            fsWord.Close();
            fsWord.Dispose();

        }

我试过这个,但是不行:

var query = dtSolicRel.AsEnumerable()
            .Where(x => x.DT_GERACAO == null && x.BL_RELATORIO == null).First();

            query.BL_RELATORIO = bytesPDF;
            query.DT_GERACAO = DateTime.Now;

            solic_adapt.Update(query);

            }

我该怎么办?

修改

我写了这段代码,但我没有更新这些文件:

DataSet ds = new DataSet();

DataTable dtRel = ds.Tables["POC_SOLIC_RELATORIO"];

var query = dtRel.AsEnumerable()
            .Select(rel => new
            {
                dtGeracao = rel.Field<DateTime>("DT_GERACAO"),
                binario   = rel.Field<byte[]>("BL_RELATORIO")
            });

我尝试了其他代码,但仍然有错误。问题出在这一行:

if(query.BL_RELATORIO == null && query.DT_GERACAO == null)

请参阅下面的代码。

var query = (from i in dtSolicRel
                         //where i.DT_GERACAO == null && i.BL_RELATORIO == null
                         select i).ToList().First();

            if(query.BL_RELATORIO == null && query.DT_GERACAO == null)
            {
                query.BL_RELATORIO = bytesPDF;
                query.DT_GERACAO = DateTime.Now;
                solic_adapt.Update(query);
            }

我这样解决了

DataSet ds = new DataSet();

            DataTable dt = ds.Tables["POC_SOLIC_RELATORIO"];

            var rowsWithAccount = (from row in dtSolicRel.AsEnumerable()
                                  where row.Field<object>("DT_GERACAO") == null && row.Field<object>("BL_RELATORIO") == null
                                  select row).First();

            for (int i = 0; i < 1; i++)
            {
                rowsWithAccount.SetField("DT_GERACAO", DateTime.Now);
                rowsWithAccount.SetField("BL_RELATORIO", bytesPDF);
                solic_adapt.Update(rowsWithAccount);
            }

我回答,但响应消失了,然后我决定编辑帖子。

2 个答案:

答案 0 :(得分:0)

假设您正确Fill DataTable,您可以执行以下操作

DataTable dtRel = new DataTable();
var queryRows = (from dRow in dtRel.AsEnumerable()
                    select new 
                    { 
                       dtGeracao = dRow.Field<DateTime>("DT_GERACAO"),
                       binario   = dRow.Field<byte[]>("BL_RELATORIO")
                    });     

答案 1 :(得分:0)

我这样做并且工作了:

 DataSet ds = new DataSet();

            DataTable dt = ds.Tables["POC_SOLIC_RELATORIO"];

            var rowsWithAccount = (from row in dtSolicRel.AsEnumerable()
                                  where row.Field<object>("DT_GERACAO") == null && row.Field<object>("BL_RELATORIO") == null
                                  select row);

            foreach (DataRow row in rowsWithAccount)
            {
                row.SetField("DT_GERACAO", DateTime.Now);
                row.SetField("BL_RELATORIO", bytesPDF);
                solic_adapt.Update(row);
            }