简体   繁体   English

如何编写Web服务以将MySQL连接到Windows应用商店?

[英]How to write a web service to connect MySQL to a windows store application?

I have developed a Windows store app in C# and I have a MySQL database(workbench) that provides data for the store app. 我已经用C#开发了一个Windows商店应用程序,并且我有一个MySQL数据库(工作台),该数据库为商店应用程序提供数据。 I have connected these directly without any web service. 我已经直接连接了这些,而没有任何Web服务。 But now I want to introduce a web service between them. 但是现在我想在它们之间引入一个Web服务。 I have developed the store app using Visual studio 2012. I have no ides on how a web service looks like or where it is written. 我使用Visual Studio 2012开发了商店应用程序。我对Web服务的外观或编写方式没有任何想法。 I browsed a lot through the web. 我在网上浏览了很多东西。 I have just a vague idea that I have to write this web service and consume it in my store app. 我有一个模糊的想法,我必须编写此Web服务并在我的商店应用中使用它。 But it is not clear on where to write and how to write. 但是尚不清楚在哪里写以及如何写。

I have only got samples for the SQL server which is not helpful for me. 我只有用于SQL Server的示例,这对我没有帮助。 Can anyone provide me proper directions and steps as to how to write this web service, Where to write it and How to access it in my Windows store app? 谁能为我提供有关如何编写此Web服务,在何处编写以及如何在Windows应用商店应用中访问它的正确指导和步骤?

This is what I tried out in my service1.asmx.cs file: 这是我在service1.asmx.cs文件中尝试过的内容:

namespace WebService1 { 命名空间WebService1 {

/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public DataSet GetStudents()
{
     using (MySqlConnection connection = new MySqlConnection 
     (ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
     {
         string Query = "SELECT * FROM [student]";
         MySqlCommand command = new MySqlCommand(Query, connection);
         command.CommandType = CommandType.Text;           
         connection.Open();
         MySqlDataReader reader = command.ExecuteReader();

         DataTable StuTable = new DataTable("student");
         StuTable.Columns.Add("id", typeof(string));
         StuTable.Columns.Add("password", typeof(string));
         StuTable.Columns.Add("courseid", typeof(string));           

        while (reader.Read())
        {
             StuTable.Rows.Add(new object[]
             {
                  reader["id"].ToString(), reader["password"].ToString(), 
                  reader["courseid"].ToString()});
             }

             StuTable.AcceptChanges();
             DataSet ds = new DataSet();
             ds.Tables.Add(StuTable);
             ds.AcceptChanges();
             return ds;
        }
}


}

} }

But I had no idea how to use this in my Store app. 但是我不知道如何在我的商店应用中使用它。

Web service is just some dynamically generated data(sql-query response in your case), that can be accessed or altered via standalone web-request to remote server from client application and in some rare cases just by user in browser window. Web服务只是一些动态生成的数据(在您的情况下为sql查询响应),可以通过独立的Web请求从客户端应用程序访问远程服务器或在少数情况下仅由用户在浏览器窗口中进行访问或更改。 It can be very strict and standardized(like SOAP), little restricted(like REST), or not restricted in any way(as most of user-made API). 它可以是非常严格和标准化的(如SOAP),几乎不受限制(如REST),也可以不受任何限制(如大多数用户制作的API)。

As first step, you must define to yourself what kind of actions your web service must do - the methods of your service, and what they must(if needed) return. 第一步,您必须自己定义Web服务必须执行的操作-服务的方法以及它们必须返回的内容(如果需要)。

At second step you need to implement this "methods" on real web-server using any of programming language where you do requests to real database(select's, updates, insert's and so on..). 第二步,您需要使用对实际数据库(选择,更新,插入等)进行请求的任何一种编程语言,在实际的Web服务器上实现此“方法”。 You not restricted in any way - you can write web-service in node.js or php, python or whatever else, service can be placed on server working under Windows, Linux, or be "in cloud" - is totally fine. 您没有任何限制-您可以在node.js或php,python或其他任何语言中编写Web服务,可以将服务放置在Windows,Linux或在“云”中运行的服务器上-完全可以。

At third step you must replace methods that you want to separate from client-part with stubs that will make requests to your "web-service" placed on some remote web-server. 在第三步中,您必须用存根替换要与客户端分离的方法,存根将向放置在某个远程Web服务器上的“ Web服务”发出请求。

Basically, this is all magic about this scary "web-service" thing. 基本上,这是关于这可怕的“ Web服务”的所有魔术。 Easy isn't? 容易不是吗? It's more just "marketing word" than some "new awesome revolutionary piece of technology" :) 它不仅仅是“营销字眼”,而不是一些“令人敬畏的革命性新技术” :)

But writing big web-service just from scratch may be hard and very boring, so there are many pieces of code that written by companies and individuals in purpose to abstract and simplify process of implementing web services... Namespace System.Web.Services in .NET Framework - is one of this pieces, where "web-service" is just a public class that based on System.Web.Services.WebService, in which can be placed public methods with [WebMethod] attribute. 但是仅从头开始编写大型Web服务可能很困难而且很无聊,因此公司和个人编写了许多段代码,目的是抽象和简化实现Web服务的过程... Namespace System.Web.Services .NET Framework-就是其中之一,其中“ web-service”只是基于System.Web.Services.WebService的公共类,可以在其中放置[WebMethod]属性的公共方法。 Just make and write new project from Visual Studio "Web Service" template - it's fun and easy :) 只需从Visual Studio“ Web服务”模板制作并编写新项目-既有趣又容易:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ASP.NET Web应用程序连接到Windows Service应用程序 - connect ASP.NET web application to Windows Service application .net-如何将Windows服务与systray应用程序连接 - .net - how to connect windows service with systray application 以 windows 服务运行 web 应用程序时拒绝连接到 localhost - Refused to connect to localhost when running web application as windows service 我了解如何将 web mysql 数据库与 C# Z0F4137ED1502B5045D6083AA258B 应用程序连接起来 - I understand how to connect web mysql database with C# windows form application Windows应用程序上的PHP Web Service与mySQL Connector - PHP Web Service vs mySQL Connector on Windows Application 如何从客户端应用程序访问Windows服务上托管的Web服务? - How to access a Web Service hosted on a Windows Service from a client application? 如何增加Windows Service应用程序中的Web服务超时? - How to increase the web service timeout in a Windows Service application? 如何编写 Web API、自托管、Windows 服务 - How to write Web API, Self hosting, Windows Service Web服务Windows窗体应用程序 - Web service windows form application 如何在Android应用程序中调用.net Web服务进行存储和检索? - How to call the .net web service in Android Application to store and retrive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM