how to add Database Connection in C# ??
Hello,My C# code is working with my localhost database connection... but how i can connect the same code to my databese server.. As im using SURFTOWN my host provider...
PLease help me to work through external server..
regards,
Here is my database class..
using System;
using MySql.Data.MySqlClient;
using System.Data;
using System.Windows.Forms;
using System.IO;
namespace LGHS
{
/// <summary>
/// Summary Description for DataBase.
/// <summary>
public class DataBase
{
#region constructor
public DataBase()
{
setDatabase = "lghs";
setUsername = "root";
setPassword = "";
}
#endregion
#region Attributes
private string server;
private string database;
private string username;
private string password;
#endregion
#region Get Methods
public string getServer
{
get{return this. server;}
}
public string getDatabase
{
get{return this.database;}
}
public string getUsername
{
get{return this.username;}
}
public string getPassword
{
get{return this.password;}
}
#endregion
#region Set Methods
public string setServer
{
set{this.server = value;}
}
public string setDatabase
{
set{this.database = value;}
}
public string setUsername
{
set{this.username = value;}
}
public string setPassword
{
set{this.password = value;}
}
#endregion
#region selectQuery
public void selectQuery(DataSet ds, string sql)
{
// Sending sql statement to database and return the dataset
try
{
// Connection string for database
string connection = "";
connection = "server="+getServer+";username="+getUsername+";password="+getPassword+";database="+getDatabase+";";
// Data adapter is used for getting data out, adapter open and close the connection to database it self
MySqlDataAdapter mysqlda = new MySqlDataAdapter(sql,connection);
// Filling dataset with data
mysqlda.Fill(ds);
}
catch(Exception e)
{
// Showing messagebox with error message
string errortext = "";
errortext = e.Message.ToString();
MessageBox.Show(errortext,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
#region iensertUpdateDeleteQuery
public void insertUpdateDeleteQuery(string sql)
{
// Running an sql statement to database
try
{
// Connection string for database
string connection = "";
connection = "server="+getServer+";username="+getUsername+";password="+getPassword+";database="+getDatabase+";";
// Creating a command and connection instance
MySqlCommand mysqlcom = new MySqlCommand(sql);
MySqlConnection mysqlcon = new MySqlConnection(connection);
// Tells the command the instance which connection it should use
mysqlcom.Connection = mysqlcon;
// Open connection to database
mysqlcon.Open();
// Running sql
mysqlcom.ExecuteNonQuery();
// Clossing connection to database
mysqlcon.Close();
}
catch(Exception e)
{
// Showing messagebox with error message
string errortext = "";
errortext = e.Message.ToString();
MessageBox.Show(errortext,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
}
}