Mysqldatabase connexion
-
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 :)
-
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 :)
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
-
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 :)
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."
-
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
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
-
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 :)