0

I'm working on a c# project and I have created a Database with EntityFramework.

This is the Database:

public partial class BDDInterneEntities : DbContext
{
    public BDDInterneEntities()
        : base("name=BDDInterneEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<CapitalisationActuelle> CapitalisationActuelle { get; set; }
    public DbSet<DonneesDUMP> DonneesDUMP { get; set; }
    public DbSet<PMRQTOTMGPS> PMRQTOTMGPS { get; set; }
    public DbSet<Resultat> Resultat { get; set; }
}

This is my main function:

public partial class MainWindow : Window
{
    private BDDInterneEntities cnn = new BDDInterneEntities();
    public MainWindow()
    {
        SampleSolution(cnn.CapitalisationActuelle, cnn.DonneesDUMP, cnn.Resultat);

    }

    private void SampleSolution(DbSet<CapitalisationActuelle> cap, DbSet<DonneesDUMP> don, DbSet<Resultat> res)
    {
        foreach (var donneesDump in don)
        {
            if (!cap.Any(c => c.PMRQTOTM == donneesDump.PMRQTOTM))
            {
                var result = cap.Any(c => don.Any(c1 => c1.PMRQTOTM == c.PMRQTOTM));
            }
            res.SqlQuery("INSERT INTO resultat VALUES 'CapitalisationActuelle'" && donneesDump.Groupe_D_alerte &&");

        }


    }

On the method SampleSolution, I'm trying to perform a

DbSet<Resultat>.SqlQuery("INSERT INTO resultat VALUES 'CapitalisationActuelle'" && donneesDump.Groupe_D_alerte &&")

The problem is, I cannot use the && to insert variable values into my table Resultat.

Somebody knows how to insert, on c#, severals variable datas into the table Resultat?

Thanks in advance, Hope I gave enough details.

Greetings.

Kraenys
  • 297
  • 1
  • 5
  • 19

2 Answers2

1

I finally foud what the request looks like.

res.SqlQuery("INSERT INTO resultat (NomTable,Groupe_D_Alerte,NomChamp,TOTMPMRQ,SiModifie,LibelléTOTAvant,LibelléTOTApres,Remarque) VALUES 'CapitalisationActuelle', '"+donneesDump.Groupe_D_alerte+"'");

&& doesn't work but + works. Moreover, we have to add ' ' around the variable.

Kraenys
  • 297
  • 1
  • 5
  • 19
0

first i think you should use cnn.relusltat.Add(entity); and then cnn.savechanges(); instead of res.SqlQuery("INSERT INTO resultat VALUES 'CapitalisationActuelle'" && donneesDump.Groupe_D_alerte &&");

which will do the work fine.

second issue is with syntax res.SqlQuery("INSERT INTO resultat VALUES ('CapitalisationActuelle'" && donneesDump.Groupe_D_alerte &&");

Ajay Kumar
  • 2,031
  • 2
  • 13
  • 17
  • I just have a question. cnn.Resultat.Add(entity); I have never used this syntax, have you got a link or something like that which could explain me this? – Kraenys Aug 26 '14 at 08:58
  • If I take this example: using (var context = new BloggingContext()) { var blog = new Blog { Name = "ADO.NET Blog" }; context.Blogs.Add(blog); context.SaveChanges(); } I suppose blog bloc contain the request and give the Name column of Blog "ADO.NET Blog", isn't it? – Kraenys Aug 26 '14 at 09:05
  • yes, it is, you can google entity framework and can find a lot of stuff on this – Ajay Kumar Aug 26 '14 at 10:02