none
facing Problem regarding Remote Connection to Database... RRS feed

  • Question

  • Hello Every one,

    I am new in C# and i created a POS previously, now i want to improve it and database can be placed at any server and others can access the database through remoting, i need help in doing this, i have written some code but its not working well,

    Here is my Code

    try
                {

    string connString = "datasource = localhost; port = 3306; username = root; password = ;";

                    MySqlConnection conn = new MySqlConnection(connString);
                    MySqlCommand command = conn.CreateCommand();

                    command.CommandText = "SELECT * FROM shdeulling.login where Username ='shedulling'";
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    string k=null;
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        k = reader.GetString(1);
                    }
                    if (k.Equals(string2))
                    {
                        t = true;
                        return t;
                    }
                    else
                    {
                        t = false;
                        conn.Close();
                        return t;
                    }
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                return t;
            }


    Saturday, August 15, 2015 8:20 AM

Answers

  • "Remoting" is either significant or perhaps a misleading word here.

    It has a specific sort of meaning to me in that you can run a session on a different machine and remote onto it. Your session runs on that machine and the screen you see is translated from there via the network to your monitor.

    Not many systems use remoting now.

    I should think you mean connect from a client machine to a database which is on a server.

    I guess you found a connection string like here:

    http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

    And based yours on that.

    Most samples are written by people who are using just their machine to put it together - so their database is on their machine.

    That's why most you see use localhost.

    localhost means it'll be on whatever machine the code runs on. You need the name of your server instead.

    From

    http://www.functionx.com/mysqlnet/csharp/Lesson02.htm

    I've edited the relevant part here:

    "

    The Source of Data

    To establish a connection, you must specify the      computer you are connecting to, that has MySQL installed. To indicate the      computer you are connecting to, use the Data Source, the DataSource,      the Host, the Server, the Addr, the Address,      or the Network Address attribute of the connection string. If you      are connecting to a local database (installed in the same computer where      the application is running), you can omit specifying the name of the      computer - you can assign localhost to this attribute.      Here is an example

    private void btnLoad_Click(object sender, System.EventArgs e) { MySqlConnection cnnVideos = new MySqlConnection("Data Source=ServerName;"); }

    "


    • Marked as answer by King Sibghat Friday, August 21, 2015 4:33 AM
    Sunday, August 16, 2015 8:30 AM
  • Thanks for Reply Sir, but i want to make it a central Position for all computers on a network, still i will be using localhost for this issue ??

    "localhost" refers to the local computer. If the database is located on remote server you need to replace "localhost" with the host name or IP address of this server:

    string connString = "Server = SERVER_NAME; Database = YOUR_DB_NAME; Uid = root; Pwd = password;";

    But once again, how to connect to a MySQL server is not a WPF topic so this forum is not the right place to ask any such questions.

    Also, MySQL is a third-party product and if you have any questions or issues about how to set it up you should ask here: http://forums.mysql.com/. These forums are intended for Microsoft's products and technologies only.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by King Sibghat Friday, August 21, 2015 4:27 AM
    Sunday, August 16, 2015 8:03 PM

All replies

  • What does "not working well" mean exactly? What exception message are you getting and on which line does it occur? And what does this issue has to do with WPF?

    Anyway, you could try to modify your connection string. Here you will find valid connection strings for a MySQL database: http://www.connectionstrings.com/mysql/

    You should specify the name of your database to begin with:

    string connString = "Server = localhost; Database = YOUR_DB_NAME; Uid = root; Pwd = password;";
                    
                    MySqlConnection conn = new MySqlConnection(connString);
                    MySqlCommand command = conn.CreateCommand();
    
                    command.CommandText = "SELECT * FROM shdeulling.login where Username ='shedulling'";
                    try
                    {
                        conn.Open();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    string k = null;
                    MySqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        k = reader.GetString(1);
                    }
                    if (k.Equals(string2))
                    {
                        t = true;
                        return t;
                    }
                    else
                    {
                        t = false;
                        conn.Close();
                        return t;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                return t;
    


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    Saturday, August 15, 2015 8:30 AM
  • This isn't a wpf question, you'd do better in the c# forum since it's sort of a generic database ado kind of a question.

    .

    This

    string connString = "datasource = localhost; port = 3306; username = root; password = ;";

    Says look for the database on the local machine then specifies a port.

    One of those must definitely be wrong since you won't be using a port at all to read something off the same machine.


    As I said though, this isn't an appropriate forum for your question and I suggest you re-post elsewhere.

    I would also recommend you explain rather more about what's happening.



    • Edited by Andy ONeill Saturday, August 15, 2015 8:34 AM
    Saturday, August 15, 2015 8:32 AM
  • Thanks for Reply Sir, but i want to make it a central Position for all computers on a network, still i will be using localhost for this issue ??
    Saturday, August 15, 2015 5:18 PM
  • "Remoting" is either significant or perhaps a misleading word here.

    It has a specific sort of meaning to me in that you can run a session on a different machine and remote onto it. Your session runs on that machine and the screen you see is translated from there via the network to your monitor.

    Not many systems use remoting now.

    I should think you mean connect from a client machine to a database which is on a server.

    I guess you found a connection string like here:

    http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL

    And based yours on that.

    Most samples are written by people who are using just their machine to put it together - so their database is on their machine.

    That's why most you see use localhost.

    localhost means it'll be on whatever machine the code runs on. You need the name of your server instead.

    From

    http://www.functionx.com/mysqlnet/csharp/Lesson02.htm

    I've edited the relevant part here:

    "

    The Source of Data

    To establish a connection, you must specify the      computer you are connecting to, that has MySQL installed. To indicate the      computer you are connecting to, use the Data Source, the DataSource,      the Host, the Server, the Addr, the Address,      or the Network Address attribute of the connection string. If you      are connecting to a local database (installed in the same computer where      the application is running), you can omit specifying the name of the      computer - you can assign localhost to this attribute.      Here is an example

    private void btnLoad_Click(object sender, System.EventArgs e) { MySqlConnection cnnVideos = new MySqlConnection("Data Source=ServerName;"); }

    "


    • Marked as answer by King Sibghat Friday, August 21, 2015 4:33 AM
    Sunday, August 16, 2015 8:30 AM
  • Thanks for Reply Sir, but i want to make it a central Position for all computers on a network, still i will be using localhost for this issue ??

    "localhost" refers to the local computer. If the database is located on remote server you need to replace "localhost" with the host name or IP address of this server:

    string connString = "Server = SERVER_NAME; Database = YOUR_DB_NAME; Uid = root; Pwd = password;";

    But once again, how to connect to a MySQL server is not a WPF topic so this forum is not the right place to ask any such questions.

    Also, MySQL is a third-party product and if you have any questions or issues about how to set it up you should ask here: http://forums.mysql.com/. These forums are intended for Microsoft's products and technologies only.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by King Sibghat Friday, August 21, 2015 4:27 AM
    Sunday, August 16, 2015 8:03 PM