使用C#内置的Web服务从mySQL数据库检索数据 [英] Web Service built in C# to retrieve data from mySQL database

查看:153
本文介绍了使用C#内置的Web服务从mySQL数据库检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在.NET中构建Web服务,该服务将从mySQL数据库中检索数据.稍后,此Web服务将与Windows窗体结合使用,在该窗体上将显示此数据.

I am trying to build a web service in .NET, which will retrieve data from a mySQL database. This web service will later on be combined with a windows form where this data will be displayed.

到目前为止,我已经准备好数据库,已经建立了数据库和Web服务之间的连接,并且表单也已准备好.

Till now, I have the database ready, the connection between the database and the web service has been made and the form is ready as well.

但是,我无法从表本身中检索特定的信息位.谁能帮助我弄清楚下一步应该做什么?我在这个问题上搜索了很多,但是我仍然找不到关于这个问题的好教程...如果您有任何想法,也可以发布链接吗?预先感谢!

However, I am unable to retrieve particular bits of information from the table itself. Can anyone help me to figure out what my next steps should be? I have googled a lot on this issue but I was still unable to find a good tutorial regarding this issue...if you have any in mind then can you please post the link as well? Thanks in advance!

其他信息:假设有一个名为"testdata"的示例表,其中有三列("id","name","age").如何提取姓名和年龄并将其显示在表单上?

EXTRA INFORMATION: Suppose a sample table called "testdata" which has three columns in it ("id", "name", "age"). How can I extract the name and the age and display them on the form?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace WebService2
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {



        private void 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();


           //-----------------------------------------------------------
           // This is the part where I should be able to retrieve the data from the database
           //-----------------------------------------------------------               


            cnMySQL.Close();
        }


    }
}

推荐答案

创建一个用WebMethodAttribute标记的公共方法,并返回一个DataTable(如果您的使用者是.net客户端).让使用者调用该方法,然后对DataTable做任何您想做的事情.

Create a method that is public, marked with the WebMethodAttribute, and returns a DataTable (if you consumer is a .net client). Have the consumer call that method and do whatever you want with the DataTable.

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

这篇关于使用C#内置的Web服务从mySQL数据库检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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