Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using V_MP.Server.API;
- using MySql.Data;
- using MySql.Data.MySqlClient; // <- both of these MUST BE included
- namespace MySQLExample{
- public class Example : APIScript{
- public override void OnChatCommand(User player, string command){
- string[] cmd = command.Split(' '); // had a bug here, fixed now
- if(cmd[0] == "/test"){
- try{ // a lot of mysql function throw exceptions, so it's recommended to use try-catch statement
- MySqlConnection connection = new MySqlConnection(); //Creates a new connection instance
- connection.ConnectionString = "server='localhost'; uid='your username'; pwd='your password'; database='database name'"; // change these depending on your setup!
- connection.Open(); // Opens connection
- //INSERT - same with delete for example
- string username = "priitkaard"; //Your variables.
- string password = "testingmysql";
- MySqlCommand comm = connection.CreateCommand(); // command instance
- comm.CommandText = "INSERT INTO users(username, password) VALUES('"+username+"', '"+password+"')"; // query
- comm.ExecuteNonQuery();
- // done
- //SELECT
- comm.CommandText = "SELECT password FROM users WHERE username='priitkaard'"; // why would you need such query, I have no idea :D
- MySqlDataReader reader = comm.ExecuteReader(); // creates a reader instance to get data
- string passw;
- while(reader.Read()){ // this.Read() returns boolean, so if there is something to read - true, otherwise false
- passw = reader.GetString(0);
- }
- reader.Close(); //close reader
- connection.Close(); // close mysql connection.
- }catch(Exception){
- SendMessageToPlayer(player, "System", "Failed to initialise MySql...");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement