WebRef.eu  - Internet Marketing and Online Business Resources  

Home / Site Map | Contact

 

Connect to a MySQL database using C# and display the data using a MySqlDataReader - Example C# Script

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">

// Script to show use of MySqlDataReader

protected void Page_Load(object sender, EventArgs e)
{
String SampleText = "My sample text";
lblMyTestLabel.Text = SampleText;

//Technique is passing the connection string to the constructor of the MySqlConnection class

// Connection string is saved in web.config
String connStr = ConfigurationSettings.AppSettings["ConnectionString"];
// Your database field names will be different
String SqlAllMerchants = "SELECT * FROM merchants ORDER BY MerchantName ASC;";

// Can switch to this string if you want to test for errors
// String connStr = "server=XXX;uid=XXX;pwd=XXX;database=XXX";

try
{
// Technique is passing the connection string to the constructor of the MySqlConnection class
// Note you have to import the MySql.Data.MySqlClient namespace to be able to use this
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
lblMyTestLabel.Text = "Connection Opened";

// Create a command to run against the database
MySqlCommand cmd = new MySqlCommand(SqlAllMerchants);
// Specify the CommandType for the connection, in this case it's text from a string, not a stored procedure
cmd.CommandType = System.Data.CommandType.Text;
// Give the MySQLCommand a connection to use
cmd.Connection = conn;

// Read from the database
MySqlDataReader MerchantsReader = cmd.ExecuteReader();

// Create a temporary string that you can add the data to
String MerchantsTemp = "";

// Read the data and add it to the temporary string. My column names are MerchantName, MerchantId etc, yours will be different
while (MerchantsReader.Read())
{
MerchantsTemp += MerchantsReader["MerchantName"].ToString();
MerchantsTemp += "<br />";
MerchantsTemp += MerchantsReader["MerchantId"].ToString();
MerchantsTemp += "<br />";
MerchantsTemp += MerchantsReader["AffLink"].ToString();
MerchantsTemp += "<br />";
MerchantsTemp += MerchantsReader["MerchantShortDesc"].ToString();
MerchantsTemp += "<br />";
MerchantsTemp += MerchantsReader["MerchantLongDesc"].ToString();
MerchantsTemp += "<br />";
MerchantsTemp += MerchantsReader["CommissionRate"].ToString();
MerchantsTemp += "<br /><br />";
}

// Set the text of a label control to the temporary string, so you can see it on the webpage. The label control appears in the HTML body.
lblMyTestLabel.Text = MerchantsTemp;

conn.Close();
}

catch (MySql.Data.MySqlClient.MySqlException ex)
{
lblMyTestLabel.Text = ex.Message;
}

 

}

</script>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
<title></title>
</head>
<body>

<asp:Label ID="lblMyTestLabel" runat="server" Text="My Test Label."></asp:Label>

</body>
</html>

References

Back to Example C# Scripts

Visit LowPrices.co.uk for Your UK Shopping


 




Low Prices UK Shopping

Compare Prices
at LowPrices.co.uk


Home / Site Map | Contact

All Content ©2020 WebRef.eu