Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Mysqldatabase connexion

Mysqldatabase connexion

Scheduled Pinned Locked Moved C#
helpjavascriptdatabasemysqldesign
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pierre besquent
    wrote on last edited by
    #1

    Hi guys, I create a formular in my .js file.I create a mysql databes with navicat 8 editor.I wanna that when i enter my data in the formular and click in the save button,it will be saved in the database.For that,in my .aspx.cs file i have this code:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.Odbc;

    public partial class application_Application : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    switch (this.Request ["Action"])
    {
    case "SaveData":
    {
    string nom = this.Request ["Name"];
    string CIN = this.Request ["CarteIdentité"];
    string Mat = this.Request ["Matricule"];
    //Cration de la connexion et de la Command
    string ConnectionString= "Driver={MySQL ODBC 3.51 Driver};"
    + "Server=localhost;" + "Database="informations";" + " UID=root;" + "PASSWORD=root";
    OdbcConnection conn=new OdbcConnection(ConnectionString);
    string requete=??????
    OdbcCommand ODC=new OdbcCommand(requete,conn);
    OdbcDataReader ODR;
    conn.Open();
    ODR=ODC.ExecuteReader();
    //Affichage du DataReader
    //traitement du datareader voir dans les sources
    //Fermeture du datareader
    ODR.Close();
    conn.Close();
    }break;
    default:{}break;
    }
    }
    }

    my problem is how i will write in "string requete= " to send the information to my database table Ty for u help :)

    U OriginalGriffO R 3 Replies Last reply
    0
    • P Pierre besquent

      Hi guys, I create a formular in my .js file.I create a mysql databes with navicat 8 editor.I wanna that when i enter my data in the formular and click in the save button,it will be saved in the database.For that,in my .aspx.cs file i have this code:

      using System;
      using System.Data;
      using System.Configuration;
      using System.Collections;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Web.UI.HtmlControls;
      using System.Data.Odbc;

      public partial class application_Application : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {
      switch (this.Request ["Action"])
      {
      case "SaveData":
      {
      string nom = this.Request ["Name"];
      string CIN = this.Request ["CarteIdentité"];
      string Mat = this.Request ["Matricule"];
      //Cration de la connexion et de la Command
      string ConnectionString= "Driver={MySQL ODBC 3.51 Driver};"
      + "Server=localhost;" + "Database="informations";" + " UID=root;" + "PASSWORD=root";
      OdbcConnection conn=new OdbcConnection(ConnectionString);
      string requete=??????
      OdbcCommand ODC=new OdbcCommand(requete,conn);
      OdbcDataReader ODR;
      conn.Open();
      ODR=ODC.ExecuteReader();
      //Affichage du DataReader
      //traitement du datareader voir dans les sources
      //Fermeture du datareader
      ODR.Close();
      conn.Close();
      }break;
      default:{}break;
      }
      }
      }

      my problem is how i will write in "string requete= " to send the information to my database table Ty for u help :)

      U Offline
      U Offline
      Umair Feroze
      wrote on last edited by
      #2

      Your query will be look like the following:

      string requete="INSERT INTO TableName(Column1, Column2, column3)
      VALUES('" + nom +"', '" + CIN + "', '" + Mat +"')";

      and then finally

      ODR=ODC.ExecuteNonQuery();

      Hope this helps

      P 1 Reply Last reply
      0
      • P Pierre besquent

        Hi guys, I create a formular in my .js file.I create a mysql databes with navicat 8 editor.I wanna that when i enter my data in the formular and click in the save button,it will be saved in the database.For that,in my .aspx.cs file i have this code:

        using System;
        using System.Data;
        using System.Configuration;
        using System.Collections;
        using System.Web;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Web.UI.WebControls.WebParts;
        using System.Web.UI.HtmlControls;
        using System.Data.Odbc;

        public partial class application_Application : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {
        switch (this.Request ["Action"])
        {
        case "SaveData":
        {
        string nom = this.Request ["Name"];
        string CIN = this.Request ["CarteIdentité"];
        string Mat = this.Request ["Matricule"];
        //Cration de la connexion et de la Command
        string ConnectionString= "Driver={MySQL ODBC 3.51 Driver};"
        + "Server=localhost;" + "Database="informations";" + " UID=root;" + "PASSWORD=root";
        OdbcConnection conn=new OdbcConnection(ConnectionString);
        string requete=??????
        OdbcCommand ODC=new OdbcCommand(requete,conn);
        OdbcDataReader ODR;
        conn.Open();
        ODR=ODC.ExecuteReader();
        //Affichage du DataReader
        //traitement du datareader voir dans les sources
        //Fermeture du datareader
        ODR.Close();
        conn.Close();
        }break;
        default:{}break;
        }
        }
        }

        my problem is how i will write in "string requete= " to send the information to my database table Ty for u help :)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Umair Feroze is correct, but I would strongly recommend you use a parametrised query instead of adding the string together:

        string requete="INSERT INTO TableName(Column1, Column2, column3) VALUES(@NOM, @CIN, @MAT)";
        ...
        ODC.Parameters.Add("@NOM", nom);
        ODC.Parameters.Add("@CIN", CIN);
        ODC.Parameters.Add("@MAT", Mat);

        Particularly with a web based application, there is a high risk of an deliberate or accidental SQL Injection attack which non-parametrised queries are wide open too.

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • U Umair Feroze

          Your query will be look like the following:

          string requete="INSERT INTO TableName(Column1, Column2, column3)
          VALUES('" + nom +"', '" + CIN + "', '" + Mat +"')";

          and then finally

          ODR=ODC.ExecuteNonQuery();

          Hope this helps

          P Offline
          P Offline
          Pierre besquent
          wrote on last edited by
          #4

          Hi Umair, I tried u solution.It runs me an error"informations doesn't exist in the current context",informations is the name of my database" my mysqlconnexion string is:

          string ConnectionString= "Driver={Navicat 8 for MySQL};"
          + "Server=localhost;" + "Database="+informations+";" + " UID=root;" + "PASSWORD=root";

          Also the

          ODR=ODC.ExecuteNonQuery();

          u tell me to add,what is its utility?where can I place it? ty

          1 Reply Last reply
          0
          • P Pierre besquent

            Hi guys, I create a formular in my .js file.I create a mysql databes with navicat 8 editor.I wanna that when i enter my data in the formular and click in the save button,it will be saved in the database.For that,in my .aspx.cs file i have this code:

            using System;
            using System.Data;
            using System.Configuration;
            using System.Collections;
            using System.Web;
            using System.Web.Security;
            using System.Web.UI;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;
            using System.Web.UI.HtmlControls;
            using System.Data.Odbc;

            public partial class application_Application : System.Web.UI.Page
            {
            protected void Page_Load(object sender, EventArgs e)
            {
            switch (this.Request ["Action"])
            {
            case "SaveData":
            {
            string nom = this.Request ["Name"];
            string CIN = this.Request ["CarteIdentité"];
            string Mat = this.Request ["Matricule"];
            //Cration de la connexion et de la Command
            string ConnectionString= "Driver={MySQL ODBC 3.51 Driver};"
            + "Server=localhost;" + "Database="informations";" + " UID=root;" + "PASSWORD=root";
            OdbcConnection conn=new OdbcConnection(ConnectionString);
            string requete=??????
            OdbcCommand ODC=new OdbcCommand(requete,conn);
            OdbcDataReader ODR;
            conn.Open();
            ODR=ODC.ExecuteReader();
            //Affichage du DataReader
            //traitement du datareader voir dans les sources
            //Fermeture du datareader
            ODR.Close();
            conn.Close();
            }break;
            default:{}break;
            }
            }
            }

            my problem is how i will write in "string requete= " to send the information to my database table Ty for u help :)

            R Offline
            R Offline
            RaviRanjanKr
            wrote on last edited by
            #5

            Hi Pierre as OriginalGriff suggested you a perfect way to save data in MYSQL i will also suggest you to use Parametrized query against database(MYSQL) you can take a look there[^] and there[^] to learn how to use parametrized query to prevent SQL Injection Attack.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups