Outils pour utilisateurs

Outils du site


bases_de_donnees:mysql:toc

Ceci est une ancienne révision du document !


MySQL

Pour l'administration de MySQL.

MySQL est un système de gestion de base de données (SGDB). Selon le type d'application, sa licence est libre ou propriétaire. Il fait partie des logiciels de gestion de base de données les plus utilisés au monde, autant par le grand public (applications web principalement) que par des professionnels, au même titre que Oracle ou SQL Server 1).

Types de données

Entiers

Type Bytes Signé Non-signé
TINYINT 1 -128 → 127 0 → 255
SMALLINT 2 -32768 → 32767 0 → 65535
MEDIUMINT 3 -8388608 → 8388607 0 → 16777215
INT 4 -2147483648 → 2147483647 0 → 4294967295
BIGINT 8 -9223372036854775808 → 9223372036854775807 0 → 18446744073709551615

Opérations

Créer une base de données

CREATE DATABASE `new_vid`
  CHARACTER SET `utf8`
  COLLATE `utf8_general_ci`;

Donner les privilèges pour l'accès à distance

> USE nom_bd;
> GRANT ALL privileges ON *.* TO 'stephane'@'%' IDENTIFIED BY 'l9s-jDhb9_SQ';
> FLUSH privileges;

Utilisation avec ADO.NET

Insérer un enregistrement

public void InsertMySQL(string city, int country_id) { 
    MySqlConnection oConn = new MySqlConnection("Database=prometo;Data Source=localhost;User id=root;Password=root"); 
    oConn.Open(); 
 
    MySqlCommand command = new MySqlCommand("insert into city(city, country_id ) select ?city, ?country_id", oConn); 
 
    command.Parameters.Add(new MySqlParameter("?city", MySqlDbType.VarChar, 50)); 
    command.Parameters.Add(new MySqlParameter("?country_id", MySqlDbType.Int16)); 
    command.Parameters[0].Value = city; 
    command.Parameters[1].Value = country_id; 
    command.ExecuteNonQuery(); 
 
    oConn.Close(); 
} 

Insérer un fichier (BLOB)

public void Mysql_File_Save(string sConnString, int nCustId, byte[] bData, string sName, string sContentType, int nContentLength) {
    using (MySqlConnection oConn = new MySqlConnection(sConnString)) {
        oConn.Open();
 
        MySqlCommand oCommand = oConn.CreateCommand();
        oCommand.Connection = oConn;
 
        //Add new 
        oCommand.CommandText = "insert into cust_file(customer_id, filename, filedata, contenttype, length) " + 
            "values( ?in_customer_id, ?in_filename, ?in_filedata, ?in_contenttype, ?in_length)";
 
        MySqlParameter oParam = oCommand.Parameters.Add("?in_customer_id", MySqlDbType.Int64);
        oParam.Value = nCustId;
        oParam = oCommand.Parameters.Add("?in_filename", MySqlDbType.VarChar, 255);
        oParam.Value = sName;
        oParam = oCommand.Parameters.Add("?in_filedata", MySqlDbType.Blob);
        oParam.Value = bData;
        oParam = oCommand.Parameters.Add("?in_contenttype", MySqlDbType.VarChar, 255);
        oParam.Value = sContentType;
        oParam = oCommand.Parameters.Add("?in_length", MySqlDbType.Int64);
        oParam.Value = nContentLength;
 
        oCommand.ExecuteNonQuery();
        oConn.Close();
    }
}

Il faut pouvoir prendre le fichier et le mettre dans un tableau.

byte[] bData = FileToArray("f:\\n1jpg.jpg"); 
Mysql_File_Save("Database=dbfichiers;Data Source=localhost;User id=root;Password=root", 
              1, bData, "N1jpg", MimeType("f:\\n1jpg.jpg"), bData.Length); 

Lire les données BLOB

public DataTable GetData_MySQLBinary() { 
    MySqlConnection oConn = new MySqlConnection(); 
    oConn.ConnectionString = "Database=fichiers;Data Source=localhost;User id=root;Password=root"; 
    oConn.Open(); 
 
    DataSet oDataSet = new DataSet("tab1"); 
    //Get data 
    MySqlCommand command = new MySqlCommand("select * from cust_file", oConn); 
 
    MySqlDataAdapter oAdapter =  new MySqlDataAdapter(command); 
    oAdapter.Fill(oDataSet, "tab1"); 
    DataTable dt = oDataSet.Tables["tab1"]; 
    oConn.Close(); 
 
    return dt;
} 
DataTable dt = GetData_MySQLBinary(); 
if (dt.Rows.Count > 0) { 
    byte[] bData2 = (byte [])dt.Rows[0]["filedata"]; 
    System.IO.MemoryStream oStream = new System.IO.MemoryStream(bData2); 
    pictureBox1.Image = Image.FromStream(oStream); 
} 

Charset

alter table tablename charset=utf8;

Ressources

bases_de_donnees/mysql/toc.1522507238.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)