我有两个工作的网络方法,一方面从客户数据库读取数据,另一方面另一方面将数据写入另一个客户数据库。
我想将这些方法连接在一起以创建批量数据流。
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";
}
}
接下来应该做什么?有线索吗?
非常感谢。
答案 0 :(得分:0)
首先将方法实现移动到两个新类中:
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;
}
}
refactor获取客户网站方法:
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方法执行相同操作。然后你可以重用这两个新类。
答案 1 :(得分:0)
您必须编写客户端脚本来调用Web服务。
以下链接对您有所帮助。
https://msdn.microsoft.com/en-us/library/bb398998(v=vs.90).aspx
您也可以通过一些ajax调用来调用Web服务。