//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;
Buy the Samsung Galaxy S25 Ultra from Three:
- Powerful octa-core processor for snappy performance.
- Galaxy AI Assistant helps you get things done quickly.
-
Pro-level photography in your pocket