Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET WebService
hi!
how can i connect web service(in C#) to mysql db.
in windows application,i have an example:
 
MySql.Data.MySqlClient.MySqlConnection conn;
string myConnectionString;
 
myConnectionString = "server=127.0.0.1;uid=root;" +
    "pwd=12345;database=test;";
 
try
{
    conn = new MySql.Data.MySqlClient.MySqlConnection();
    conn.ConnectionString = myConnectionString;
    conn.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
    MessageBox.Show(ex.Message);
}
 
i need an example in web service.
tnx
Posted 11-Mar-13 3:26am
Comments
ryanb31 at 11-Mar-13 8:30am
   
The same way. Do you know how to create a webservice? Maybe you need to start with that.
mohsenadc at 11-Mar-13 8:38am
   
i have a webservice now.
i need search in mysql db in a function.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

something like this:
    [System.Web.Services.WebMethod]
    public DataTable connectoToMySql()
    {
        string connString = "SERVER=localhost" + ";" +
            "DATABASE=testdatabase;" +
            "UID=root;" +
            "PASSWORD=password;";
 
        MySqlConnection cnMySQL = new MySqlConnection(connString);
 
        MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
 
        MySqlDataReader reader;
 
        cmdMySQL.CommandText = "select * from testdata";
 
        cnMySQL.Open();
 
        reader = cmdMySQL.ExecuteReader();
 
        DataTable dt = new DataTable();
        dt.Load(reader);
 

        cnMySQL.Close();
 
        return dt;
    } 
 
Cheers,
Edo
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100