【问题标题】:C# process data from one webmethod to anotherC# 将数据从一种 Web 方法处理到另一种
【发布时间】:2015-12-30 10:48:12
【问题描述】:

我有两个有效的网络方法,一个从客户数据库读取数据,另一个将数据写入另一个客户数据库。

我想将这些方法连接在一起以创建批量数据流。

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "namespace")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    DataClassesDataContext dc = new DataClassesDataContext();




    [WebMethod]
    public string GetCustomer(String CustomerName, String CustomerSurName, String CustomerAddress, String CustomerEmail, String CustomerPhone,
        String Barcode, String StoreName, String City, String Town, String BirthDay, String CreateDate, String Signature,
        String IsProcessed, String CreateDateHour, String IsChecked, String IsEmpty)
    {
        var json = "";
        var getcustomer = from result in dc.CustomerInfos
                          where result.CustomerName == CustomerName
                         select result;

        JavaScriptSerializer jss = new JavaScriptSerializer();
        json = jss.Serialize(getcustomer);

        return json;
    }


    public int MyConnectionString2 { get; private set; }

    [WebMethod]
    public string insertCustomer (String CustomerName, String CustomerSurName, String CustomerAddress, String CustomerEmail, String CustomerPhone,
        String Barcode, String StoreName, String City, String Town, String BirthDay, String CreateDate, String Signature,
        String IsProcessed, String CreateDateHour, String IsChecked, String IsEmpty)
    {


        string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString2"].ConnectionString;

        SqlConnection sqlCon = new SqlConnection(connectionString);
        SqlCommand nonqueryCommand = sqlCon.CreateCommand();
        sqlCon.Open();
        nonqueryCommand.CommandText = "INSERT  INTO CustomerInfos (CustomerName, CustomerSurName, CustomerAddress, CustomerEmail, CustomerPhone, Barcode, StoreName, City, Town, BirthDay, CreateDate, Signature, IsProcessed, CreateDateHour, IsChecked, IsEmpty) VALUES (@CustomerName, @CustomerSurname, @CustomerAddress, @CustomerEmail, @CustomerPhone, @Barcode, @StoreName, @City, @Town, @BirthDay, @CreateDate, @Signature, @IsProcessed, @CreateDateHour, @IsChecked, @IsEmpty)";
        // Add Parameters to Command Parameters collection
        nonqueryCommand.Parameters.Add("@CustomerName", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@CustomerSurName", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@CustomerAddress", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@CustomerEmail", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@CustomerPhone", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@Barcode", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@StoreName", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@City", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@Town", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@BirthDay", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@CreateDate", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@Signature", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@IsProcessed", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@CreateDateHour", SqlDbType.VarChar, 50);
        nonqueryCommand.Parameters.Add("@IsChecked", SqlDbType.VarChar, 100);
        nonqueryCommand.Parameters.Add("@IsEmpty", SqlDbType.VarChar, 100);


        nonqueryCommand.Parameters["@CustomerName"].Value = CustomerName;
        nonqueryCommand.Parameters["@customerSurname"].Value = CustomerSurName;
        nonqueryCommand.Parameters["@CustomerAddress"].Value = CustomerAddress;
        nonqueryCommand.Parameters["@CustomerEmail"].Value = CustomerEmail;
        nonqueryCommand.Parameters["@CustomerPhone"].Value = CustomerPhone;
        nonqueryCommand.Parameters["@Barcode"].Value = Barcode;
        nonqueryCommand.Parameters["@StoreName"].Value = StoreName;
        nonqueryCommand.Parameters["@City"].Value = City;
        nonqueryCommand.Parameters["@Town"].Value = Town;
        nonqueryCommand.Parameters["@BirthDay"].Value = BirthDay;
        nonqueryCommand.Parameters["@CreateDate"].Value = CreateDate;
        nonqueryCommand.Parameters["@Signature"].Value = Signature;
        nonqueryCommand.Parameters["@IsProcessed"].Value = IsProcessed;
        nonqueryCommand.Parameters["@CreateDateHour"].Value = CreateDateHour;
        nonqueryCommand.Parameters["@IsChecked"].Value = IsChecked;
        nonqueryCommand.Parameters["@IsEmpty"].Value = IsEmpty;


        nonqueryCommand.ExecuteNonQuery();
        return "done";


    }

}

接下来我该怎么做?有什么线索吗?

非常感谢。

【问题讨论】:

  • 所以你想选择客户并将其插入到另一个数据库中?
  • 确实,我有两个数据库服务器和两个 Web 方法,我几乎尝试了所有方法。将这两个 Web 方法分离为两个 Web 服务。如何从 getCustomer 消费并使用 insertCustomer 写入?

标签: c# asmx webmethod


【解决方案1】:

首先将方法实现移动到两个新类中:

public class SourceCustomerRepository
  {

    public CustomerInfo GetCustomer(String CustomerName, String CustomerSurName, String CustomerAddress, String CustomerEmail, String CustomerPhone,
        String Barcode, String StoreName, String City, String Town, String BirthDay, String CreateDate, String Signature,
        String IsProcessed, String CreateDateHour, String IsChecked, String IsEmpty)
    {
      return from result in dc.CustomerInfos
      where result.CustomerName == CustomerName
      select result;
    }

  }

将获取客户网络方法重构为:

var customerRepo=new SourceCustomerRepository();
    JavaScriptSerializer jss = new JavaScriptSerializer();
      var customer = customerRepo.GetCustomer(CustomerName, CustomerSurName, CustomerAddress, CustomerEmail,
        CustomerPhone, Barcode, StoreName, City, Town, BirthDay, CreateDate, Signature, IsProcessed, CreateDateHour,
        IsChecked, IsEmpty);
      json = jss.Serialize();

      return json;

对 insertCustomer 方法做同样的事情。然后你可以重用这两个新类。

【讨论】:

  • 感谢您的快速回复,但我无法做到。先生,我该如何设置您的方法?
【解决方案2】:

您必须编写客户端脚本来调用 Web 服务。

这是对您有帮助的链接。

https://msdn.microsoft.com/en-us/library/bb398998(v=vs.90).aspx

您也可以通过一些 ajax 调用来调用 Web 服务。

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多