Já fiz alguns updates com linq sem problema. mas da forma como está meu código, estou tendo dificuldade. Qual a regra:
1) Tenho uma tela onde eu faço algumas solicitações de relatórios. Logo eu posso ter n solicitações em andamento.
2) Essas solicitações, eu preencho alguns campos, exceto dois campo: DT_GERACAO
e BL_RELATORIO
. BL_RELATORIO deve conter um arquivo binário(campo LOB-Oracle), que é o relatório em si(Um array de bytes gerado na minha aplicação).
3) Aí quando eu chamar o relatório, ele dever pegar o primeiro registro que encontrar e popular esse dois campos null
, onde a DT_GERACAO
recebe DateTime.Now()
e o BL_RELATORIO
recebe o arquivo binário gerado, porém apenas um por vez, ou seja, cada vez que eu executar o relatório, ele busca na tabela em questão e vê se os dois campos citados estão null
e então popula. A aplicação que gera o relatório, é uma aplicação console e não tenho nenhum contexto nela, apenas dataset. Veja o código abaixo do gerador de relatório.
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);
dtSolicRel.Where(p => p.DT_GERACAO == null && p.BL_RELATORIO == null).First();
//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();
if(dtSolicRel.Count > 0)
{
var query = (from qr in dtSolicRel
select qr).ToList();
}
}
tentei fazer nessas linhas o meu update, mas não está dando certo. Não consigo trazer os campos na var query para atualizar:
if(dtSolicRel.Count > 0)
{
var query = (from qr in dtSolicRel
select qr).ToList();
}
bytesPDF é o binário que deverá ser gravado na tabela. A tabela se chama: POC_SOLIC_RELATORIO
. O dataset se chama: dsPlanoMedico
e ele já contém todas as tabelas necessárias(4). Usamos ReportViewer.
Essa aqui é a condição para o update:
dtSolicRel.Where(p => p.DT_GERACAO == null && p.BL_RELATORIO == null).First();
Não testei ainda. Fiz isso, mas estou com dúvidas de como gravar no BD, pois não tenho contexto e estou uma baita dúvida em como fazer com DataSet.
dtSolicRel.Where(p => p.DT_GERACAO == null && p.BL_RELATORIO == null).First();
if (dtSolicRel.Count > 0 && bytesPDF.Length > 0)
{
var query = dtSolicRel.AsEnumerable().Where(x => x.DT_GERACAO == null && x.BL_RELATORIO == null).First();
query.BL_RELATORIO = bytesPDF;
query.DT_GERACAO = DateTime.Now;
}
EDITADO ABAIXO
Estou fazendo isso e vou testar. O que eu achei estranho é que não aceitou o DateTime.Now(), mas assim: DateTime.Now.
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);
Tentei fazer isso, mas está dando erro devido testar null, mas não vejo outra forma.
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);
}