anand kulkarni

anand kulkarni

  • NA
  • 99
  • 48.8k

Calling Db connection class and getting table data to textbo

Jan 10 2014 6:58 AM
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;
}
}

Answers (1)