To the below code I have tried to get some table values . As for the same condition means while loading the from and through the class . Means when I get the successful connection while load the form same time I want pull out mysql table values to text box . I Have tried but after spending some hours I posted it here . Connection class is same as below
I am using below class .
Regards
Aanand S. Kulkarni
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using MySql.Data.MySqlClient;
namespace ConnectCsharpToMysql
{
class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
private string woday;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
private void Initialize()
{ server = "localhost";
database = "bank";
uid = "myuid";
password = "mypwd";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
// private bool OpenConnection()
internal bool OpenConnection()
{
try
{
connection.Open();
string query = "SELECT * FROM mainautho ";
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
woday = (dataReader["custname"] + "");
MessageBox.Show(woday);
}
//close Data Reader
dataReader.Close();
MessageBox.Show(woday);
return true;
//return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
internal bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
Loading

Sunny SharmaPosted Jan 11, 2014, 6:04 AM
Your code looks fine but database handling is not in proper way. Also, you haven't specified what problem is there you're getting after running this code.
However, using a connection in this way is not advisable. Observe the pattern below:
----------------------------------------------------------------------------------
Add reference to "System.Data" (I'll explain why I've added this)
I assume ther's already a MySql connection "MySqlConnection".
internal string GetCustomerName()
{
string customerName="";
try
{
MySqlConnection.Open(); //try to open the connection and catch if any error occurs.
}
catch(Exception ex) // you should place specific exception types
{
// log/display db connection error;
return; // no further operations can be performed since there's no connection exist.
}
//ConnectionState Enum belong to System.Data namespace, hence I referenced it above.
if(MySqlConnection.State==ConnectionState.Open)
{
//connection is open, perform db operation here.
customerName="x";
}
if(MySqlConnection.State==ConnectionState.Open)
{
//db operation is over, close the connection.
try
{
MySqlConnection.Close();
}
Catch{}
}
return customerName;
}
----------------------------------------
HTH :)
------------------------------------------------------------