2016-03-23 395 views
4
string GetSerials = "SELECT SerialNumber from Warranty"; 
      string TestUpdateDates = "UPDATE Warranty SET StartDate = '@StartDate', EndDate = '@EndDate' WHERE SerialNumber = '@result'"; 
      //string TestUpdateDates2 = "UPDATE Warranty SET StartDate = cDate(Format('@StartDate', 'MM/dd/yyyy')), EndDate = cDate(Format('@EndDate', 'MM/dd/yyyy')) WHERE(SerialNumber = '@result')"; 

DataTable dataTable = new DataTable(); 
    using (var conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Blah\Blah\Blah\Warranty.accdb")) 
    using (OleDbCommand serialCommand = new OleDbCommand(GetSerials, conn1)) 
    { 
    conn1.Open(); 
    dataTable.Load(serialCommand.ExecuteReader()); 

    foreach (DataRow row in dataTable.Rows) 
    { 
     var result = row["SerialNumber"].ToString(); 
     WebRequest request = WebRequest.Create("urlpart1" + result + "urlpart2"); 
     string json; 
     var response = request.GetResponse(); 
     request.ContentType = "application/json; charset=utf-8"; 

     using (var streamr = new StreamReader(response.GetResponseStream())) 
     using (OleDbCommand testupdateCommand = new OleDbCommand(TestUpdateDates, conn1)) 
     using (OleDbCommand updateCommand = new OleDbCommand(UpdateDates, conn1)) 
     using (OleDbCommand deleteCommand = new OleDbCommand(DeleteIncomplete, conn1)) 
     { 
     json = streamr.ReadToEnd(); 
     List<MyObject> list = JsonConvert.DeserializeObject<List<MyObject>>(json); 
     MyObject obj = list[0]; //Base Warranty 

     // obj -- Base Warranty 
     var StartDate = obj.Start; 
     var EndDate = obj.End; 

     //testupdateCommand.Parameters.Add("@StartDate", OleDbType.Date).Value = StartDate; 
     //testupdateCommand.Parameters.Add("@EndDate", OleDbType.Date).Value = EndDate; 
     testupdateCommand.Parameters.AddWithValue("@StartDate", StartDate); 
     testupdateCommand.Parameters.AddWithValue("@EndDate", EndDate); 
     testupdateCommand.Parameters.AddWithValue("@result", result); 

我想用JSON響應中的值更新我的數據庫。我想遍歷列[[SerialNumber]]中的所有值並使用該序列號,我可以從url獲得開始日期結束日期。我想將這些值存儲到同一個序列號中,我曾用它將url分配到各自的字段(StartDate,EndDate)中。這個循環爲什麼不循環?

我已經註釋了一些其他的方式,我試圖做到這一點,但我得到的輸出是,只有我得到的第一個SerialNumber被更改,它不會繼續通過我的列循環100個值。如果我添加

MessageBox.Show(result); //SerialNumber 
MessageBox.Show(StartDate.ToString()); 
MessageBox.Show(EndDate.ToString()); 

它通過MessageBox.Show進行循環,但它不會以這種方式更新我的數據庫。爲什麼它本身不重複?

我使用ACCESS2013如果

+1

更新查詢中錯誤的是非常重要的。刪除參數佔位符周圍的單引號。這樣它們不是參數的標記,而是字符串。這段代碼有沒有空的嘗試/抓住? – Steve

+0

這些單引號只是一個實驗,哈哈我刪除了它們並嘗試它,但仍然沒有成功。我沒有圍繞代碼嘗試/捕獲 – Havoux

+0

你的方法太長了 - 將你的函數分解成更小的組件,並從主方法(或相互之間)調用它們。這將使您能夠獨立測試各個部分,並啓用重用。 –

回答

2

不要引用參數:)

string GetSerials = "SELECT SerialNumber from Warranty"; 
       string TestUpdateDates = "UPDATE Warranty SET StartDate = @StartDate, EndDate = @EndDate WHERE SerialNumber = @result"; 
       //string TestUpdateDates2 = "UPDATE Warranty SET StartDate = cDate(Format(@StartDate, 'MM/dd/yyyy')), EndDate = cDate(Format(@EndDate, 'MM/dd/yyyy')) WHERE(SerialNumber = @result)"; 

    DataTable dataTable = new DataTable(); 
     using (var conn1 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Blah\Blah\Blah\Warranty.accdb")) 
     using (OleDbCommand serialCommand = new OleDbCommand(GetSerials, conn1)) 
     { 
     conn1.Open(); 
     dataTable.Load(serialCommand.ExecuteReader()); 

     foreach (DataRow row in dataTable.Rows) 
     { 
      var result = row["SerialNumber"].ToString(); 
      WebRequest request = WebRequest.Create("urlpart1" + result + "urlpart2"); 
      string json; 
      var response = request.GetResponse(); 
      request.ContentType = "application/json; charset=utf-8"; 

      using (var streamr = new StreamReader(response.GetResponseStream())) 
      using (OleDbCommand testupdateCommand = new OleDbCommand(TestUpdateDates, conn1)) 
      using (OleDbCommand updateCommand = new OleDbCommand(UpdateDates, conn1)) 
      using (OleDbCommand deleteCommand = new OleDbCommand(DeleteIncomplete, conn1)) 
      { 
      json = streamr.ReadToEnd(); 
      List<MyObject> list = JsonConvert.DeserializeObject<List<MyObject>>(json); 
      MyObject obj = list[0]; //Base Warranty 

      // obj -- Base Warranty 
      var StartDate = obj.Start; 
      var EndDate = obj.End; 

      //testupdateCommand.Parameters.Add("@StartDate", OleDbType.Date).Value = StartDate; 
      //testupdateCommand.Parameters.Add("@EndDate", OleDbType.Date).Value = EndDate; 
      testupdateCommand.Parameters.AddWithValue("@StartDate", StartDate); 
      testupdateCommand.Parameters.AddWithValue("@EndDate", EndDate); 
      testupdateCommand.Parameters.AddWithValue("@result", result);