c# 检测 mysql 服务,使用C#内置的Web服务从mySQL数据库检索数据

本文档提供了一个.NET Web服务示例,该服务连接到MySQL数据库并检索数据。内容涉及建立数据库连接、创建SQL查询以及如何将数据加载到DataTable中。用户遇到的问题是在获取数据后无法在Windows表单上展示特定信息,如'name'和'age'。解决方案是创建一个公开的Web方法,返回一个包含所需数据的DataTable,供Windows表单调用并显示。
摘要由CSDN通过智能技术生成

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.

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!

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

}

}

}

解决方案

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值