i did connect c# with mysql xampp but my app does not write and read from databas >>>?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

i did connect c# with mysql xampp but my app does not write and read from databas >>>??

String connectionstring = "datasource = localhost;username = wail;password =; database = wail"; string query = "INSERT INTO `name`(`jobs`) VALUES(1258)"; MySqlConnection DBconnection = new MySqlConnection(connectionstring); DBconnection.Open(); MySqlCommand mySqlCommend = new MySqlCommand(query, DBconnection); DBconnection.Close();

26th Jan 2018, 12:30 AM
ALAMOUDI, WAEL MAKKI S
ALAMOUDI, WAEL MAKKI S - avatar
2 Answers
+ 2
you have connected to the database and disconnected well but forgot to execute the query. call mySqlCommend.ExecuteNonQuery() before closing the connection. don't forget to enclose this snippets in a try block and let the catch block receives the errors if any. come back of you got any errors
26th Jan 2018, 1:33 AM
Sujith k sukumar
Sujith k sukumar - avatar
+ 1
This is because on line 4 of your code, you are creating a command object and instantiating it with your query and db connection, but you are not doing anything with it after that. This line is just defining your command. You need to call one of the execute methods to actually run your query on the database. See this link for the definition of the methods available to you. https://dev.mysql.com/doc/dev/connector-net/6.10/html/T_MySql_Data_MySqlClient_MySqlCommand.htm
26th Jan 2018, 1:38 AM
Michelle Dougenik
Michelle Dougenik - avatar