2016-01-20 37 views
1

我正在嘗試使用foreach循環將數據添加到數據庫中,以遍歷包含不同類型值的列表。用foreach循環將不同類型的列表插入到SQL Server數據庫中

List<string> productCodeList = new List<string>(); 
List<string> productNameList = new List<string>(); 
List<string> productPriceList = new List<string>(); 
List<int> competitorList = new List<int>(); 
List<DateTime> dateCreatedList = new List<DateTime>(); 

有沒有辦法通過不同類型的多個列表進行迭代?因爲目前我只能將一個列表插入到一列中。我想要的是將數據插入指定的所有列。

或者是否有更好的方法呢?

using (var con = new SqlConnection(connectionString)) 
{ 
    con.Open(); 

    using (var cmd = new SqlCommand(@"INSERT INTO OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, Price, DateCreated) 
       VALUES(@CompetitorID, @ProductCode, @ProductName, @Price, @DateCreated)", con)) 
    { 
     cmd.Parameters.Add("@CompetitorID", SqlDbType.Int); 
     cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar); 
     cmd.Parameters.Add("@ProductName", SqlDbType.VarChar); 
     cmd.Parameters.Add("@Price", SqlDbType.Decimal); 
     cmd.Parameters.Add("@DateCreated", SqlDbType.DateTime); 

     foreach (var value in competitorList) 
     { 
      cmd.Parameters["@CompetitorID"].Value = value; 
      int rowsAffected = cmd.ExecuteNonQuery(); 
     } 

     competitorList.Clear(); 
    } 
} 

在此先感謝!

回答

1

我想你想是這樣的:

for (int i = 0; i < competitorList.Count; i++) 
{ 
    cmd.Parameters["@CompetitorID"].Value = competitorList[i]; 
    cmd.Parameters["@ProductCode"].Value = productCodeList[i]; 
    cmd.Parameters["@ProductName"].Value = productNameList[i]; 
    // etc 
    int rowsAffected = cmd.ExecuteNonQuery(); 
} 
1

讓他們在這樣一個單獨的列表,使得它真的很難確定哪些產品代碼與哪個產品名稱。

我會建議構建一個dto來處理這個問題。喜歡的東西:

public class Products 
{ 
public string productCodeList {get; set;} 
public string productNameList {get; set;} 
public string productPriceList {get; set;} 
public int competitorList {get; set;} 
public DateTime dateCreatedList {get; set;} 
} 

比你可以使用:

List<Products> products = new List<Products>(); 

或者你可以只建全的DataTable在您的代碼,並使用SqlBulckCopy將其插入到你的數據庫。

DatTable

Quick Example of Datatables

SqlBulkCopy

2

你的邏輯相關,如姓名,密碼等分成多個列表像@Santiago指出,這將是難以維護,而且容易出錯的屬性。

如果您完全控制了代碼,您可以考慮創建一個Product類並擁有一系列產品。

public class Product 
{ 
    public string Code { get; set; } 
    public string Name { get; set; } 
    public decimal Price { get; set; } 
    public int CompetitorId { get; set; } 
    public DateTime DateCreated { get; set; } 
} 

public class DemoClass 
{ 
    public static void Demo() 
    { 
     var products = new List<Product>(); 

     // fill the list here 

     using (var con = new SqlConnection(connectionString)) 
     { 
      con.Open(); 
      using (var cmd = new SqlCommand(@"INSERT INTO OnlineProductsTemp$(CompetitorID, ProductCode, ProductName, Price, DateCreated) 
      VALUES(@CompetitorID, @ProductCode, @ProductName, @Price, @DateCreated)", con)) 
      { 
       cmd.Parameters.Add("@CompetitorID", SqlDbType.Int); 
       cmd.Parameters.Add("@ProductCode", SqlDbType.VarChar); 
       cmd.Parameters.Add("@ProductName", SqlDbType.VarChar); 
       cmd.Parameters.Add("@Price", SqlDbType.Decimal); 
       cmd.Parameters.Add("@DateCreated", SqlDbType.DateTime); 
       foreach (var p in products) 
       { 
        cmd.Parameters["@CompetitorID"].Value = p.CompetitorId; 
        cmd.Parameters["@ProductCode"].Value = p.Code; 
        cmd.Parameters["@ProductName"].Value = p.Name; 
        cmd.Parameters["@Price"].Value = p.Price; 
        cmd.Parameters["@DateCreated"].Value = p.DateCreated; 

        var rowsAffected = cmd.ExecuteNonQuery(); 
       } 
       products.Clear(); 
      } 
     } 
    } 
} 
相關問題