2013-10-22 75 views
2

我有下面的代碼,它不更新數據庫。該代碼正在做它想要做的事情,但是當我ExecuteNonQuery()它不保存任何東西回數據庫。連接字符串沒有問題,因爲它與正在工作的select查詢相同。C#ExecuteNonQuery不更新數據庫

const string str = @"Data Source=localhost;Initial Catalog=Items;Integrated Security=True"; 
     static void Main(string[] args) 
     { 
      const string connectionString = str; 

      DataTable DataTableAllWithoutID = new DataTable(); 
#if !test 
      string queryString = "select * from table_items_shelves;"; 
      SqlDataAdapter adapterAllWithoutID = new SqlDataAdapter(queryString, connectionString); 
      adapterAllWithoutID.Fill(DataTableAllWithoutID); 
      adapterAllWithoutID.Dispose(); 
      SqlConnection connection = new SqlConnection(connectionString); 
      connection.Open(); 
      string insertString = "Update table_items_shelves Set Item_ID = @Item_ID where Item_Name = '@key';"; 
      SqlCommand insertCommand = new SqlCommand(insertString, connection); 
      insertCommand.Parameters.Add("@Item_ID", SqlDbType.Int); 
      insertCommand.Parameters.Add("@key", SqlDbType.NVarChar); 
#else 
      DataTableAllWithoutID.Columns.Add("Item_Name", typeof(string)); 
      DataTableAllWithoutID.Columns.Add("Item_ID", typeof(object)); 
      foreach (List<object> row in input) 
      { 
       DataRow newRow = DataTableAllWithoutID.Rows.Add(); 
       newRow.ItemArray = row.ToArray(); 
      } 

#endif 
      //this code will get empty items 
      List<DataRow> nullOrEmpty = DataTableAllWithoutID.AsEnumerable() 
       .Where(x => x.Field<object>("Item_ID") == null) 
       .ToList(); 
      //this creates a dictionary of valid items 
      Dictionary<int, List<DataRow>> dict = DataTableAllWithoutID.AsEnumerable() 
       .Where(x => x.Field<object>("Item_ID") != null) 
       .GroupBy(x => x.Field<object>("Item_ID"), x => x) 
       .ToDictionary(x => Convert.ToInt32(x.Key), x => (List<DataRow>)x.ToList()); 
      //create IEnumerator for the null items 
      IEnumerator<DataRow> emptyRows = nullOrEmpty.GetEnumerator(); 
      Boolean noMoreEmptyRows = false; 
      if (emptyRows != null) 
      { 
       foreach (int key in dict.Keys) 
       { 
        Console.WriteLine(key.ToString()); 
        //get count of items    
        int count = dict[key].Count; 
        int itemID = (int)key; 
        for (int index = count; count < 8; count++) 
        { 
         if (emptyRows.MoveNext()) 
         { 
          //get an item from the null list     
          emptyRows.Current["Item_ID"] = itemID; 
          insertCommand.Parameters["@Item_ID"].Value = itemID; 
          insertCommand.Parameters["@key"].Value = emptyRows.Current["Item_Name"]; 
          insertCommand.ExecuteNonQuery(); 
          Console.WriteLine("current item ID " + itemID); 
          Console.WriteLine("current count " + count); 
          //Console.ReadKey(); 
         }//end if 
         else 
         { 
          noMoreEmptyRows = true; 
          break; 
         }//end else 
        }//end for 
        if (noMoreEmptyRows) 
         break; 
       }//end foreach 
       if (!noMoreEmptyRows) 
       { 
        //increment key to one greater than max value 
        int maxKey = dict.Keys.Max() + 1; 
        int count = 0; 
        while (emptyRows.MoveNext()) 
        { 
         //get an item from the null list     
         emptyRows.Current["Item_ID"] = maxKey.ToString(); 
         insertCommand.Parameters["@Item_ID"].Value = maxKey.ToString(); 
         insertCommand.Parameters["@key"].Value = emptyRows.Current["Item_ID"]; 

          insertCommand.ExecuteNonQuery(); 

          count++; 
         if (count == 8) 
         { 
          maxKey++; 
          count = 0; 
         } 
        } 
       } 
      }//end if 
#if test 
      foreach (DataRow row in DataTableAllWithoutID.Rows) 
      { 
       Console.WriteLine("Item_Name : {0} Item ID : {1}", 
        row["Item_Name"], row["Item_ID"]); 
      } 
#endif 

      FIllGroupID(str); 
      Console.ReadKey(); 

     } 
+0

爲什麼當它真的是一個'UPDATE' ??時它被稱爲'InsertString'! –

+0

@marc_s現在正在插入它正在更新。我必須改變它。 – mubi

+0

是的,我肯定會改變它 - 總是考慮*最小驚喜的原則* - 如果它被稱爲**'插入.....',我希望它**可以與'Insert '而不是完全不同的...... –

回答

8
where Item_Name = '@key'; 

這是字面'@key'匹配 - 參數@key。你可能想要:

where Item_Name = @key; 
+0

工作感謝,我很幸運沒有得到這個負面投票。 – mubi

+0

@mubi meh;它可以說是一個特定的場景,但至少你包括所有相關信息來回答它 –