2015-12-30 63 views
1

我有兩個可用的webmethods,一個從客戶數據庫讀取數據,另一個則將數據寫入另一個客戶數據庫。C#從一個webmethod處理數據到另一個

我想將這些方法連接在一起以創建批量數據流。

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

事實上,我有兩個數據庫服務器和兩個webmethods,我已經嘗試了幾乎所有的東西。將這兩個webmethods分成兩個web服務。我如何從getCustomer中使用並使用insertCustomer進行編寫? –

回答

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

    } 

重構得到客戶的網絡方法:

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方法相同。然後你可以重用這兩個新類。

+0

謝謝你的快速回復,但我無法做到這一點。我該如何確定你的方法先生? –

相關問題