Click here to Skip to main content
11,705,451 members (35,070 online)
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 2: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 Sergey Alexandrovich Kryukov 525
1 OriginalGriff 244
2 CPallini 82
3 ppolymorphe 76
4 Inimicos 60
0 OriginalGriff 8,988
1 Sergey Alexandrovich Kryukov 8,272
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2015
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