public ActionResult MetaSync() { List <DataSync> metaItems = new List <DataSync>(); for (int metaTableIndex = 0; metaTableIndex < formsSql.GetNumberOfMetaTables(); metaTableIndex++) { DataSync ds = new DataSync { ID = metaTableIndex, tableName = formsSql.GetMetaDataTableName(metaTableIndex), SISOnlineCnt = 0, VentureCnt = formsSql.GetTableRecordCountVenture(formsSql.GetMetaDataTableName(metaTableIndex)), SyncStatus = String.Empty }; CookieContainer cc = new CookieContainer(); try { string lnk = ConfigurationManager.AppSettings["SISOnlineUrl"] + "Defws/Login?UserId=" + SessionHelper.LoginInfo.LoginID + "&pwrd=" + SessionHelper.LoginInfo.Password; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(lnk); request.Timeout = -1; request.CookieContainer = cc; Session["CookieContainer"] = cc; using (WebResponse response = request.GetResponse()) { Debug.WriteLine("DataSync HttpWebResponse Status: " + ((HttpWebResponse)response).StatusDescription); using (Stream dataStream = ((HttpWebResponse)response).GetResponseStream()) { } } } catch (Exception xcptn) { Debug.WriteLine("DataSync HttpWebRequest: " + xcptn.Message); } try { HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(sisOnlineURL + "DataSync/GetMetaDataTableCount?fileId=" + metaTableIndex.ToString()); httpRequest.Method = WebRequestMethods.Http.Get; httpRequest.CookieContainer = cc; // Get back the HTTP response for web server using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { using (Stream httpResponseStream = httpResponse.GetResponseStream()) { Debug.WriteLine("DataSync.MetaSync HttpWebResponse Status: " + httpResponse.StatusDescription); byte[] rsp = new byte[httpResponse.ContentLength]; httpResponseStream.Read(rsp, 0, (int)httpResponse.ContentLength); int onlineCnt = Convert.ToInt32(System.Text.Encoding.UTF8.GetString(rsp)); ds.SISOnlineCnt = onlineCnt; ds.VentureCnt = formsSql.GetTableRecordCountVenture(formsSql.GetMetaDataTableName(metaTableIndex)); } } } catch (System.Net.WebException wex) { ds.SyncStatus = "SIS Server not found."; Debug.WriteLine("* * * DataSync index exception " + wex.Message); } catch (Exception excptn) { ds.SyncStatus = excptn.Message; Debug.WriteLine("* * * DataSync index exception: " + excptn.Message); } metaItems.Add(ds); } DataSync dsc = new DataSync(); ViewBag.MetaCnt = metaItems.Count; ViewBag.MetaTable = metaItems; return(View("metaSync", dsc)); }
/// <summary> /// Processes the long running action. /// </summary> /// <param name="id">The id.</param> public string ProcessMetaDataSync(string id) { int rspCnt = 0; int metaTableCnt = formsSql.GetNumberOfMetaTables(); lock (syncRoot) { ProcessStatus[id] = 1; } try { // Delete all meta table first for (int DeleteOrderId = metaTableCnt - 1; DeleteOrderId >= 0; DeleteOrderId--) { rspCnt = formsSql.DeleteTableContent(formsSql.GetMetaDataTableName(DeleteOrderId)); // ProcessStatus[id] = 2, 3, 4 lock (syncRoot) { ProcessStatus[id] += 1; } } for (int fileId = 0; fileId < metaTableCnt; fileId++) { // 2. Execute the URL to run the method on the remote server: // Export/GetResponsesCsv?fileId=1 // Construct HTTP request to get the file HttpWebRequest httpRequest = (HttpWebRequest) WebRequest.Create(sisOnlineURL + "Export/GetMetaCsv?fileId=" + fileId.ToString()); httpRequest.Method = WebRequestMethods.Http.Get; DataTable csvData = new DataTable(); // Get back the HTTP response for web server using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse()) { Stream httpResponseStream = httpResponse.GetResponseStream(); // ProcessStatus[id] = 5, 7, 9 lock (syncRoot) { ProcessStatus[id] += 1; } using (var reader = new CsvReader(httpResponseStream)) { // the CSV file has a header record, so we read that first reader.ReadHeaderRecord(); csvData.Fill(reader); Debug.WriteLine("Table contains {0} rows.", csvData.Rows.Count); } rspCnt = csvData.Rows.Count; csvData = RemoveNullString(csvData); } // 3. Read the FileStreamResult // - Read through the records /*using (SqlBulkCopy bulkCopy = new SqlBulkCopy(formsSql.GetConnectionString(), SqlBulkCopyOptions.KeepNulls | SqlBulkCopyOptions.KeepIdentity)) * { * bulkCopy.DestinationTableName = formsSql.GetMetaDataTableName(fileId); * bulkCopy.ColumnMappings.Clear(); * foreach (var column in csvData.Columns) * bulkCopy.ColumnMappings.Add(column.ToString(), column.ToString()); * bulkCopy.WriteToServer(csvData); * }*/ csvData.TableName = formsSql.GetMetaDataTableName(fileId); DbConnection connection = UasAdo.GetUasAdoConnection(); FillDatabaseTable(csvData, connection); // ProcessStatus[id] = 6, 8, 10 lock (syncRoot) { ProcessStatus[id] += 1; } } } catch (Exception ex) { Debug.WriteLine("* * * DataSync exception: " + ex.Message); } return(id); }