how to retrieve the data from mysql to visual studio 2017 using c shar
Loading
how to retrieve the data from mysql to visual studio 2017 using c shar
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 2:09 AM EDT
Follow the steps
1)Install the MySQL Connector/NET using NuGet Package Manager:
2) C# code
using System; using MySql.Data.MySqlClient; namespace MySQLDataRetrieval { class Program { static void Main(string[] args) { // Replace the placeholders with your MySQL server details string connectionString = "your connection string here"; // Step 3: Write C# Code using (MySqlConnection connection = new MySqlConnection(connectionString)) { try { connection.Open(); // SQL query to retrieve data string query = "SELECT * FROM your_table_name;"; MySqlCommand command = new MySqlCommand(query, connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // Access the data using reader methods based on the data types int id = reader.GetInt32("id"); string name = reader.GetString("name"); int age = reader.GetInt32("age"); // Do something with the retrieved data Console.WriteLine($"ID: {id}, Name: {name}, Age: {age}"); } reader.Close(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } } }Vishal YelveRank #178 · 13.6K reputationPosted Jul 27, 2023, 4:33 AM EDT
Hi Pragadeeshwar,
try this
MYSQL Query
-- Database: `testdb` -- Table structure for table `student` -- CREATE TABLE IF NOT EXISTS `student` ( `Name` varchar(200) NOT NULL, `Address` varchar(200) NOT NULL, `Mobile` varchar(200) NOT NULL, `Email` varchar(200) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `student` -- INSERT INTO `student` (`Name`, `Address`, `Mobile`, `Email`) VALUES ('vishal', 'mumbai', '1234567890', '[email protected]'), ('kunal', 'pune', '9700000000', '[email protected]'), ('priya', 'delhi', '4512369870', '[email protected]');C# Code
using System; using MySql.Data.MySqlClient; namespace RetrieveSqlDataReader { class ShowData { static void Main(string[] args) { string connectionString = "Your connection string "; MySqlConnection conn = null; MySqlDataReader Reader = null; conn = new MySqlConnection(connectionString); conn.Open(); Console.WriteLine("\n Retrieving data from database MySql server\n"); try { MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM student"; Reader = cmd.ExecuteReader(); Console.WriteLine("Name|Address|Mobile|Email\n"); while (Reader.Read()) { Console.WriteLine(Reader.GetString(0) + "|" + Reader.GetString(1) + "|" + Reader.GetString(2) + "|" + Reader.GetString(3)); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.ToString()); } finally { if (Reader != null) { Reader.Close(); } if (conn != null) { conn.Close(); } } } } }