Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. using MySql.Data.MySqlClient; // Adding MySQL client functions
  9.  
  10. namespace TestingMySQL
  11. {
  12.     public partial class Default : System.Web.UI.Page
  13.     {
  14.         protected void Page_Load(object sender, EventArgs e)
  15.         {
  16.  
  17.             // Change the username, password and database according to your needs
  18.             // You can ignore the database option if you want to access all of them.
  19.             // 127.0.0.1 stands for localhost and the default port to connect.
  20.             string connectionString = "server=;port=;username=;password=;database=;";
  21.             // Your query,
  22.             string query = "SELECT * FROM user";
  23.  
  24.             // Prepare the connection
  25.             MySqlConnection databaseConnection = new MySqlConnection(connectionString);
  26.             MySqlCommand commandDatabase = new MySqlCommand(query, databaseConnection);
  27.             commandDatabase.CommandTimeout = 60;
  28.             MySqlDataReader reader;
  29.  
  30.             // Let's do it !
  31.             try
  32.             {
  33.                 // Open the database
  34.                 databaseConnection.Open();
  35.  
  36.                 // Execute the query
  37.                 reader = commandDatabase.ExecuteReader();
  38.  
  39.                 // All succesfully executed, now do something
  40.  
  41.                 // IMPORTANT :
  42.                 // If your query returns result, use the following processor :
  43.  
  44.                 if (reader.HasRows)
  45.                 {
  46.                     while (reader.Read())
  47.                     {
  48.                         // As our database, the array will contain : ID 0, FIRST_NAME 1,LAST_NAME 2, ADDRESS 3
  49.                         // Do something with every received database ROW
  50.                         string[] row = { reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3) };
  51.                     }
  52.                 }
  53.                 else
  54.                 {
  55.                     MessageBox.Text = ("No rows found.");
  56.                 }
  57.  
  58.                 // Finally close the connection
  59.                 databaseConnection.Close();
  60.             }
  61.             catch (Exception ex)
  62.             {
  63.                 // Show any error message.
  64.                 MessageBox.Text = ex.Message;
  65.             }
  66.  
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement