如何在webservice中连接到我的sql? [英] how can i connect to my sql in webservice?

查看:60
本文介绍了如何在webservice中连接到我的sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

如何将Web服务(在C#中)连接到mysql数据库。

在Windows应用程序中,我有一个例子:



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需要一个Web服务示例。

tnx



i need an example in web service.
tnx

推荐答案

类似这样的事情:

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;
}





干杯,

Edo



Cheers,
Edo

这篇关于如何在webservice中连接到我的sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆