Avatar billede sibga Nybegynder
25. februar 2012 - 21:25 Der er 5 kommentarer

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

    }
}
Avatar billede sibga Nybegynder
25. februar 2012 - 21:30 #1
When i change my constructor information to my server address.. like ...

setServer = "mydb23.surftown.se";

but it's giving error ..

Unable to connect to any of the specified MySQL Host...

please help mmeee
Avatar billede sibga Nybegynder
27. februar 2012 - 10:09 #3
I have followed the instruction and have created a remote database access with a IP and have tried to connect.. but its still saying...

Unable to connect to any of the specified MySQL Host... 

help me please..
Avatar billede keysersoze Guru
27. februar 2012 - 23:33 #4
If you have followed the instructions I think the easiest way to solve your problem is to contact surftown - maybe they can see if there are anything missing.
Avatar billede rasmuzzen Nybegynder
29. februar 2012 - 15:09 #5
try in a commandbox

telnet mydb23.surftown.se 3306

if it blanks the screen then there is access to the db server.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester