1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

connexion base de données SQL Server 2008 avec ADO.NET Mode conecté

abdelouafiOct 1, 2017

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      399
      Likes Received:
      9
      Trophy Points:
      18
      Joined
      Sep 13, 2016
      sql server 2008 ado net mode connecte.png
      Création de la base de donnée SQL Server 2008:


      Code:
      create database master
      
      use master
      create table client(
      codeclient int primary key,
      nom varchar(50)
      )
      Code de connexion à la base de données via visuel studio:
      Code:
      SqlConnection cnx = new SqlConnection(@"Data Source=ABDELOUAFI\SQLEXPRESS;Integrated Security=TRUE;Initial Catalog=master");
      
                           //ou bien - deuxième méthode
                          //SqlConnection cnx = new SqlConnection();
                          //cnx.ConnectionString = @"Data Source=ABDELOUAFI\SQLEXPRESS;";
                      //cnx.ConnectionString += @"Initial Catalog=gestion_client;Integrated Security=true";
      
                      cnx.Open();
      code source du bouton "Ajouter":
      Code:
      private void button1_Click(object sender, EventArgs e)
              {
                  //string ch = "Initial Catalog=gestion_client;integrated security=true;server=.";
                  try
                  {
          SqlConnection cnx = new SqlConnection(@"Data Source=ABDELOUAFI\SQLEXPRESS;Integrated Security=TRUE;Initial Catalog=master");
      
                 
                          //cnx.ConnectionString = @"Data Source=ABDELOUAFI\SQLEXPRESS;";
                      //cnx.ConnectionString += @"Initial Catalog=gestion_client;Integrated Security=true";
      
                      cnx.Open();
                      string req = "insert into client values(@codeclient,@nomclient)";
      
                      //les paramètres à ajouter
      
                      SqlParameter p1 = new SqlParameter("@codeclient", int.Parse(codeclient.Text));
                      SqlParameter p2 = new SqlParameter("@nomclient", nomclient.Text);
      
                      //ajouter un nouveau client
      
                      SqlCommand cmd = new SqlCommand(req, cnx);
                      //cmd.Parameters.AddRange(p1);
                      cmd.Parameters.Add(p1);
                      cmd.Parameters.Add(p2);
                      int n = cmd.ExecuteNonQuery();
                      if (n > 0)
                      {
                          MessageBox.Show("client bien ajouté");
                      }
                      else
                      {
                          MessageBox.Show("erreur d'ajout de client");
                      }
      
                      cnx.Close();
                  }
                  catch(Exception )
                  {
                      MessageBox.Show("erreur de conx");
                  }
      
             
              }
      Ajout du DatagridView, le code source de la page Form_Load est le suivant:
      Code:
      private void Form1_Load(object sender, EventArgs e)
              {
                  dataGridView1.Columns.Add("Code client", "codeclient");
                  dataGridView1.Columns.Add("Nom client", "nom");
      
              }
      Et si on veut afficher les informations de la table client (de la base de données "master") sur le DatagridView, alors le code source du bouton "Afficher" est le suivant:
      Code:
       private void afficher_Click(object sender, EventArgs e)
              {
                  SqlConnection cnx = new SqlConnection(@"Data Source=ABDELOUAFI\SQLEXPRESS;Integrated Security=TRUE;Initial Catalog=master");
                  cnx.Open();
                  string req2 = "select * from client";
                  SqlCommand cmd = new SqlCommand(req2, cnx);
                  SqlDataReader reader;
                  reader = cmd.ExecuteReader();
                  while (reader.Read())
                  {
                      dataGridView1.Rows.Add(reader["codeclient"], reader["nom"]);
                  }
              }
       
      Last edited: Oct 1, 2017
      Loading...

Share This Page

Share