2012-02-18 30 views
2

我正在創建一個Web應用程序,我需要將一列客戶端 上載到數據庫中,該列表將來自CSV,TSV或EXCEL文件。ASP.NET - MySQL批量插入每行的異步響應

這些文件可能包含50-200記錄

我需要一種方法來給出一個響應返回給客戶端的每一行插入到數據庫中。 如成功或失敗。

不希望執行所有INSERTS,然後返回結果摘要。

我只需要關於如何做到這一點的想法。它確定如果你不能提供的代碼。但是,如果可以,當然更好。

請隨時舉報或編輯或重新編號。

謝謝。最好的祝福!!!

+0

客戶端是否需要與插入進行交互?喜歡取消插入中間或什麼的? – 2012-02-18 16:04:29

+1

嗨感謝您的迴應...呃這將是很好,如果我可以添加該功能..但沒有必要..再次感謝 – 2012-02-18 16:05:47

+1

爲什麼我被拒絕? – 2012-02-18 16:08:14

回答

2

我會嘗試一個答案,並基於使用會話,JavaScript計時器和AJAX調用。

客戶端和服務器之間通信的最常見方式就像客戶端因此要求服務器尋求某些東西。這是通過一個定時器在JavaScript中實現的,當時間流逝時,進行AJAX調用並創建一個新的定時器來完成同樣的事情。

在服務器上,您啓用AJAX Methods,這些將成爲AJAX調用的入口點。該方法將響應您的插入狀態。

既然你處理插入兩個地方,一個插入頁面,另一個插入那個AJAX方法(這是靜態的,你將無法訪問Page實例),你需要移動邏輯在一個單獨的類中,並跟蹤會話中的插入。

因此,代碼如下所示:

public class CsvInserts 
{ 
    private IList<string> InsertsProgress { 
     get { 
      if (HttpContext.Current.Session["CsvInserts.Inserts"] == null) 
       HttpContext.Current.Session["CsvInserts.Inserts"] = new List<string>(); 
      return (IList<string>)HttpContext.Current.Session["CsvInserts.Inserts"]; 
     } 
    } 
    public IList<string> GetInsertsProgress() { 
     return InsertsProgress; 
    } 
    public void InsertFile(string[] lines) { 
     foreach (var line in lines) 
       { 
        var row = DataAccess.CsvInserts.Insert(line); // code to insert the line 
        InsertsProgress.Add(row.GetRelevantInfoForUser()); // successfully inserted or not and which line was inserted 
       } 
    } 
} 

在插入頁面,你會做這樣的事情

protected void btInsert_OnClick(object sender, EventArgs e) 
{ 
    var lines = .. get lines from the posted file; 
    var insertManager = new CsvInserts(); 
    insertManager.InsertFile(lines); 
} 

WebMethod能夠將這樣的

[WebMethod] 
    public static IList<string> GetInsertsProgress() 
    { 
      var insertManager = new CsvInserts(); 
      return insertManager.GetInsertsProgress(); 
    } 

在客戶端使用該計時器,您將一次又一次地調用這個方法,直到插入完成。用jQuery或其他東西顯示收到的字符串。

此C#代碼來自內存,更像是一個指南。對不起,沒有給出JavaScipt代碼。此外,您必須在插入完成時或創建新的批量插入時清除字符串列表。

+2

感謝非常詳細的回覆。根據這些邏輯,我們不得不嘗試一些東西。謝謝。很多。生病後我會回來=) – 2012-02-18 16:57:46

0

繼承人類似的,代碼compily和工作,但需要一點「工作」,我們做單線更新在哪裏,我更喜歡使用ThreadPool ASP.Net異步調用,因爲我們有問題與任務和未處理的異常等,總是編碼器故障。

// In the FileUpload Event Handler, or other Event Handler raised to start the process 
ThreadPool.QueueUserWorkItem(new WaitCallback(DataLoader.InsertClientRecords), new Tuple<string, Guid>("PathToFile.csv", uniqueIdentifier)); 


// In the Ajax callback (or WebMethod as previously proposed) to update the UI, possible update a GridView with results etc. 
List<Tuple<Guid, int, bool, string>> updates = DataLoader.GetProgressReports(uniqueIdentifier); 


// Static class 
public static class DataLoader 
{ 
    private static readonly object locker = new object(); 

    // Tuple Guid for unique Identifier, int RowNumber, bool Result, string Message (if any) 
    private static List<Tuple<Guid, int, bool, string>> results = new List<Tuple<Guid, int, bool, string>>(); 


    public static void InsertClientRecords(object stateInfo) 
    { 
     // string FilePath, Guid for unique Identifier 
     Tuple<string, Guid> recordInfo = stateInfo as Tuple<string, Guid>; 

     if (recordInfo != null) 
     { 
      string filePath = recordInfo.item1; 
      Guid id = recordInfo.item1; 

      lock (locker) 
      { 
       // Update results List 
      }    
     } 
    } 


    // Tuple Guid for unique Identifier, int RowNumber, bool Result, string Message (if any) 
    public static List<Tuple<Guid, int, bool, string>> GetProgressReports(Guid identifier) 
    { 
     List<Tuple<Guid, int, bool, string>> updatedRecords = null; 
     bool lockTaken = false; 

     try 
     { 
      // 1000 Millisecond Timeout so the Ajax callback does not hang. 
      Monitor.TryEnter(locker, 1000, ref lockTaken); 

      if (lockTaken) 
      { 
       updatedRecords = results.Where(r => (r.Item1 == identifier)).ToList(); 

       if (updatedRecords != null) 
        DataLoader.results.RemoveAll(r => (r.Item1 == identifier)); 
      } 
     } 
     finally 
     { 
      if (lockTaken) 
       Monitor.Exit(locker); 
     } 

     return updatedRecords; 
    }