INSERT SQL SERVER DataAdapter

Résolu
blackhox Messages postés 34 Date d'inscription jeudi 31 octobre 2013 Statut Membre Dernière intervention 20 août 2016 - Modifié par blackhox le 9/01/2014 à 17:11
blackhox Messages postés 34 Date d'inscription jeudi 31 octobre 2013 Statut Membre Dernière intervention 20 août 2016 - 13 janv. 2014 à 21:23
Bonjour à tous,

J'essaye d'effectuer un INSERT dans ma DB SQL Server de cette façon :


dataAdapter.InsertCommand = new SqlCommand("INSERT INTO fournisseurs VALUES (@FOURN_SOCIETE, @FOURN_ADRESSE, @FOURN_TELEPHONE, @FOURN_FAX, @FOURN_MAIL, @FOURN_SITE, @FK_PAYS_ID, @FK_LOCALITE_ID) SELECT PAYS.PAYS_ID AS FK_PAYS_ID, LOCALITE.LOCALITE_ID AS FK_LOCALITE_ID FROM PAYS, LOCALITE WHERE PAYS_NOM = @PAYS_NOM AND LOCALITE_PAYS = @LOCALITE_PAYS AND LOCALITE_CP = @LOCALITE_CP AND LOCALITE_VILLE=@LOCALITE_VILLE",connection);
dataAdapter.InsertCommand.Parameters.Add("@FOURN_SOCIETE", SqlDbType.VarChar).Value = societe;
dataAdapter.InsertCommand.Parameters.Add("@FOURN_ADRESSE", SqlDbType.VarChar).Value = adresse;
dataAdapter.InsertCommand.Parameters.Add("@FOURN_TELEPHONE", SqlDbType.VarChar).Value = telephone;
dataAdapter.InsertCommand.Parameters.Add("@FOURN_FAX", SqlDbType.VarChar).Value = fax;
dataAdapter.InsertCommand.Parameters.Add("@FOURN_MAIL", SqlDbType.VarChar).Value = mail;
dataAdapter.InsertCommand.Parameters.Add("@FOURN_SITE", SqlDbType.VarChar).Value = site;
dataAdapter.InsertCommand.Parameters.Add("@FK_PAYS_ID", SqlDbType.VarChar).Value = ??????;
dataAdapter.InsertCommand.Parameters.Add("@FK_LOCALITE_ID", SqlDbType.VarChar).Value = ??????;

Je m'embrouille avec mon INSERT INTO SELECT pour insérer dans mes FOREIGN KEY fk_pays_id et fk_localite_id.

Est ce que je dois effectué un select avant pour récupérer l'id du pays, de la localité ou je peux tout faire en une requête?

Si vous avez une solution plus simple sans utiliser dataAdapter je suis preneur également.

merci d'avance pour votre réponse.
A voir également:

2 réponses

cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 33
11 janv. 2014 à 10:29
Bonjour

Tu peux tout faire dans une même requête , mais la syntaxe est quelque peux différente, ne pas utiliser le terme "VALUES" pour les données mais directement un "SELECT".
et sans passer par un DataAdapter cela donnerait quelque chose comme:

private void InsertFournisseur(string societe, string adresse, string telephone, string fax, string mail, string site, string pays, string localitePays, string localiteCp, string localiteVille)
{
  string Query = @"
 INSERT INTO Fournisseurs (SOCIETE, ADRESSE, TELEPHONE, FAX, MAIL, SITE, PAYS_ID, LOCALITE_ID)
 SELECT @FOURN_SOCIETE, @FOURN_ADRESSE, @FOURN_TELEPHONE, @FOURN_FAX, @FOURN_MAIL,@FOURN_SITE, PAYS.PAYS_ID, LOCALITE.LOCALITE_ID
 FROM PAYS CROSS JOIN LOCALITE
 WHERE (PAYS.PAYS_NOM = @PAYS_NOM) AND (LOCALITE.LOCALITE_PAYS =@LOCALITE_PAYS) AND (LOCALITE.LOCALITE_CP = @LOCALITE_CP) AND  (LOCALITE.LOCALITE_VILLE =@LOCALITE_VILLE)";

  SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=test;Integrated Security=true");
  try
  {
    connection.Open();
    SqlCommand command = new SqlCommand(Query, connection);
    command.Parameters.AddWithValue("@FOURN_SOCIETE", societe);
    command.Parameters.AddWithValue("@FOURN_ADRESSE", adresse);
    command.Parameters.AddWithValue("@FOURN_TELEPHONE", telephone);
    command.Parameters.AddWithValue("@FOURN_FAX", fax);
    command.Parameters.AddWithValue("@FOURN_MAIL", mail);
    command.Parameters.AddWithValue("@FOURN_SITE", site);
    command.Parameters.AddWithValue("@PAYS_NOM", pays);
    command.Parameters.AddWithValue("@LOCALITE_PAYS", localitePays);
    command.Parameters.AddWithValue("@LOCALITE_CP", localiteCp);
    command.Parameters.AddWithValue("@LOCALITE_VILLE", localiteVille);
    if (command.ExecuteNonQuery() == 0)
      throw new ApplicationException("Aucune ligne insérée, vérifiez les paramètres!");
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
  finally
  {
    connection.Close();
  }
}



2
blackhox Messages postés 34 Date d'inscription jeudi 31 octobre 2013 Statut Membre Dernière intervention 20 août 2016
13 janv. 2014 à 21:23
Parfait, merci.
0
Rejoignez-nous