heroberlin Posted February 19, 2019 Share Posted February 19, 2019 Hey guys, iam pretty new to C# and dont know, how to connect to my MySQL database and do a simple select query. It would be awesome, if a person could post a simple connect and select programm code, so I can work with it, and understand how to do it. Thank you in advance. Link to comment Share on other sites More sharing options...
notorious_nils Posted February 19, 2019 Share Posted February 19, 2019 if you can read others code check the wierd players gamemode in database.cs is a good example ...... i will do a simple version for you 1 Link to comment Share on other sites More sharing options...
notorious_nils Posted February 19, 2019 Share Posted February 19, 2019 (edited) using MySql.Data.MySqlClient; namespace Database { class DatabaseTest { public string connectionString = "SERVER=127.0.0.1; DATABASE=db; UID=root; PASSWORD=pw;"; /// <summary> /// Get the Name from Database /// </summary> /// <param name="id">primary key</param> /// <returns>the name</returns> public static string GetName(int id) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); MySqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM `mytable` WHERE id = @id LIMIT 1"; command.Parameters.AddWithValue("@id", id); //command.Parameters.AddWithValue("@somemore", somemore); using (MySqlDataReader reader = command.ExecuteReader()) { reader.Read(); return reader.GetString("name"); } } } } } it is not testet but should work like this Edited February 19, 2019 by notorious_nils 1 Link to comment Share on other sites More sharing options...
heroberlin Posted February 19, 2019 Author Share Posted February 19, 2019 Thank you so much. I will try this. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now