blackhox
Messages postés36Date d'inscriptionjeudi 31 octobre 2013StatutMembreDernière intervention20 août 2016
-
Modifié par blackhox le 9/01/2014 à 17:11
blackhox
Messages postés36Date d'inscriptionjeudi 31 octobre 2013StatutMembreDernière intervention20 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.
cs_Robert33
Messages postés834Date d'inscriptionsamedi 15 novembre 2008StatutMembreDernière intervention14 janvier 201733 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();
}
}
blackhox
Messages postés36Date d'inscriptionjeudi 31 octobre 2013StatutMembreDernière intervention20 août 2016 13 janv. 2014 à 21:23
DevLama
Messages postés356Date d'inscriptionmercredi 13 avril 2011StatutMembreDernière intervention18 novembre 202110 10 janv. 2014 à 17:12
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();
13 janv. 2014 à 21:23