C#将数据从一个Web方法处理到另一个

本文关键字:Web 一个 方法 处理 另一个 数据 | 更新日期: 2023-09-27 18:28:32

我有两个工作的web方法,一个从客户数据库读取数据,另一个向另一个客户数据库写入数据。

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

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

    }
}

我下一步该怎么办?有线索吗?

非常感谢。

C#将数据从一个Web方法处理到另一个

Firs将方法实现转移到两个新类中:

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

重构getcustomerweb方法:

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方法执行相同操作。然后您可以重用这两个新类。

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

这是一个对你有帮助的链接。

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

此外,您还可以通过一些ajax调用来调用web服务。

相关文章: