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();
}
}
Salut voici une Maniere Facile pour manupiler les données avec SQL SERVER en utilisant ADO.NET
class DBconnection { public String SQL { set; get; } public SqlDataReader rdr; private SqlConnection cnx; private SqlCommand cmd;
public void GetConnectionOpened() { cnx = new SqlConnection("Data Source=localhost;Initial Catalog=exercicecsharp;Integrated Security=true"); cnx.Open(); cmd = new SqlCommand(SQL, cnx); rdr = cmd.ExecuteReader(); }
public void GetDisconnection() { rdr.Dispose(); cnx.Close(); }
//Pour inserer les données DBconnection db = new DBconnection(); db.SQL = "INSERT INTO AGENT VALUES('"+ag.ID+"','"+ag.Nom+"','"+ag.Prenom+"','"+ag.Sexe+"')"; db.GetConnectionOpened(); db.GetDisconnection();