Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using MySql.Data.MySqlClient; // Adding MySQL client functions
- namespace TestingMySQL
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- // Change the username, password and database according to your needs
- // You can ignore the database option if you want to access all of them.
- // 127.0.0.1 stands for localhost and the default port to connect.
- string connectionString = "server=;port=;username=;password=;database=;";
- // Your query,
- string query = "SELECT * FROM user";
- // Prepare the connection
- MySqlConnection databaseConnection = new MySqlConnection(connectionString);
- MySqlCommand commandDatabase = new MySqlCommand(query, databaseConnection);
- commandDatabase.CommandTimeout = 60;
- MySqlDataReader reader;
- // Let's do it !
- try
- {
- // Open the database
- databaseConnection.Open();
- // Execute the query
- reader = commandDatabase.ExecuteReader();
- // All succesfully executed, now do something
- // IMPORTANT :
- // If your query returns result, use the following processor :
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- // As our database, the array will contain : ID 0, FIRST_NAME 1,LAST_NAME 2, ADDRESS 3
- // Do something with every received database ROW
- string[] row = { reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3) };
- }
- }
- else
- {
- MessageBox.Text = ("No rows found.");
- }
- // Finally close the connection
- databaseConnection.Close();
- }
- catch (Exception ex)
- {
- // Show any error message.
- MessageBox.Text = ex.Message;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement