[C#] [MYSQL] Connect C# script to MYSQL

Thx very much :D! I’ll try this when VIsual Studio is updated :D! Is 12% now :D.

Anyway for “create a mysql manager” you mean something like that , to get access to the database?
I’m new on coding :D, soo I don’t know perfectly everyting :smiley: . Like this, but using this .dll you provided:

using System;
using System.Collections.Generic;
using MySql.Data.MySqlClient;

using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace MySqlTut
{
    public class DBConnect : BaseScript
    {


        //database stuff
        private const String SERVER = "127.0.0.1";
        private const String DATABASE = "test";
        private const String UID = "root";
        private const String PASSWORD = "";
        private static MySqlConnection dbConn;

        // User class stuff
        public int Id { get; private set; }

        public String Username { get; private set; }

        public String Password { get; private set; }

        public DBConnect(){
        }


        private DBConnect(int id, String u, String p)
        {
            Id = id;
            Username = u;
            Password = p;
        }

        public static void InitializeDB()
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            builder.Server = SERVER;
            builder.UserID = UID;
            builder.Password = PASSWORD;
            builder.Database = DATABASE;

            String connString = builder.ToString();

            builder = null;

            Console.WriteLine(connString);

            dbConn = new MySqlConnection(connString);

        }

        public static List<DBConnect> GetUsers()
        {
            List<DBConnect> users = new List<DBConnect>();

            String query = "SELECT * FROM users";

            MySqlCommand cmd = new MySqlCommand(query, dbConn);

            dbConn.Open();

            MySqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                int id = (int)reader["id"];
                String username = reader["username"].ToString();
                String password = reader["password"].ToString();

                DBConnect u = new DBConnect(id, username, password);

                users.Add(u);
            }

            reader.Close();

            dbConn.Close();

            return users;
        }

        public static DBConnect Insert(String u, String p)
        {
            String query = string.Format("INSERT INTO users(username, password) VALUES ('{0}', '{1}')", u, p);

            MySqlCommand cmd = new MySqlCommand(query, dbConn);

            dbConn.Open();

            cmd.ExecuteNonQuery();
            int id = (int)cmd.LastInsertedId;

            DBConnect user = new DBConnect(id, u, p);

            dbConn.Close();

            return user;

        }
        public void Update(string u, string p)
        {
            String query = string.Format("UPDATE users SET username='{0}', password='{1}' WHERE ID={2}", u, p, Id);

            MySqlCommand cmd = new MySqlCommand(query, dbConn);

            dbConn.Open();

            cmd.ExecuteNonQuery();

            dbConn.Close();
        }

        public void Delete()
        {
            String query = string.Format("DELETE FROM users WHERE ID={0}", Id);

            MySqlCommand cmd = new MySqlCommand(query, dbConn);

            dbConn.Open();

            cmd.ExecuteNonQuery();

            dbConn.Close();
        }
    }
}

yep exactly ^^ .

your code sample seem good , you can adapt it for your need.

if you have trouble don’t hesitate to ask me

1 Like

Don’t ask him. He’ll destroy your code and put in hacks.

Jk jk… he’s really good to ask :slight_smile: Very, very helpful. I wouldn’t have been able to get started in C# if it weren’t for him.

2 Likes

ahahah how did you notice i was stealing code ?!

ps: as promise i transfert you 50 bitcoin for that comment :wink:

1 Like

Whoaaaa… I’m sorry sir, but please read the Terms of Service for FiveM. The terms explicitly state:

No financial profit shall be derived by the Users from any derivative of, or third-party service, User Generated Content, or Game Server for FiveM, the Game Services, or other entities stated in these Terms, except as expressly permitted by CitizenFX. …

:smile::smile::smile::rofl:

Thx very much is working perfectly :D!!! It’s amazing to see a working mysql database XD.
Finally :D! Thx again :D!
I also remember you Briglair, thx also for your help :D!

don’t worry i did transfert 100 bitcoin to the manager of fivem for got the right to not respect this.

1 Like

nice to ear it man.:+1::+1:

1 Like

Only need to thx you or I will never be able :D!

304bl I have last question, what is the best way to get the steamID of a player? Soo I can start to make compatible with essentialmode my scripts.

I tried to use this:
https://wiki.fivem.net/wiki/GetPlayerIdentifiers

CitizenFX.Core.Native.API.GetPlayerIdentifier(string, int);

what I should put as parameter since in lua there is only the id ?

I can get the local ID , the server ID but I can’t get the steam ID…

@DrMagus5 On the player object, do ply.Identifiers["steam"]. It will return a string with the steam ID.

1 Like

Player p = new PlayerList()[serverIdOfPlayer];
string steamId = p.Identifiers.First();

if the player connect though steam you will get the steam id like this.

You can also do ply.Identifiers["ip"] or ply.Identifiers["license"] to get those.

Something to note, though… say your steam id is steam:45647546, it will only return 45647546. It will not include the steam:

Thx very much 304bl and Briglair , I’m using the steam id and the license to have a consistence with the essentialmode. The code you shared work perfectly :slight_smile: , thx again guys you are the best :D!

1 Like

glad to help, and don’t forget c# WILL NEVER DIE !!

hmm i don’t have Identifiers in PlayerList do i use the wrong references?

I don’t remember if correct but this work only server side. And identifier work on player not playerlist. In this case we took a player from the playerlist since is an array :wink:.
About the dll there is a dll for the client and another one for the server. One inside the server folder and another in the fivem client installation. They have the same name so you should rename one in Client and another in server when you copy them in the same folder. I’m not sure if this was the problem but I tryed to help. The bad thing is that if you want to retrieve and id doesn’t work Clint side…

thank you for your answer of course i meant players… i dont have Identifiers there!

another thing, can i just rename the net references to MySqlConnector.net.dll i.e. and it works?

About the mysql I got some errors the only way I found was to download the dll provided by 304bl : http://mind2world.com/dl/mysqlConnector.rar
Used as reference in the project and put this content in the same folder of my dll. I don’t know other ways but tecnically should work using the .net way as you said. (compiling the github.com version gived me some errors)

Hi man, the link provided by 304bl is not working anymore, do you have the files hosted, can you provide the link if so, thanks… i tryed hard to compile the github version but i seems impossible, there are missing tons of dlls