2015-10-13 56 views
0

當嘗試從SQL查詢中將結果提取到C#中的對象列表中時遇到了問題。出於某種原因,它會覆蓋最近查詢結果的所有行,我看不到我做錯了什麼。用於測試的Console.WriteLine返回正確的數據,以便查詢正常運行。將SQL結果添加到對象列表時,所有記錄都被最近的結果覆蓋

我的代碼是:

private void getData(string qry) 
    { 
     int count = 0; 
     string strProject = "ARTUR-PC\\SQLEXPRESS"; //Enter your SQL server instance name 
     string strDatabase = "Northwind"; //Enter your database name 
     string strUserID = "Artlemaks"; // Enter your SQL Server User Name 
     string strPassword = "rootUser"; // Enter your SQL Server Password 
     string strconn = "data source=" + strProject + 
     ";Persist Security Info=false;database=" + strDatabase + 
     ";user id=" + strUserID + ";password=" + 
     strPassword + ";Connection Timeout = 0"; 
     //conn = new SqlConnection(strconn); 
     using (SqlConnection connection = new SqlConnection(strconn)) 
     { 
      List<CustomerObj> Customers = new List<CustomerObj>(); 
      connection.Open(); 
      SqlCommand cmd = connection.CreateCommand(); 
      cmd.CommandText = qry; 

      cmd.ExecuteNonQuery(); 

      SqlDataReader reader = cmd.ExecuteReader(); 
      if (reader.HasRows) 
      { 
       while (reader.Read()) 
       { 
        row.custID = reader[0] as int?; 
        row.cmpName = reader[1] as string; 
        row.cntName = reader[2] as string; 
        row.cntTitle = reader[4] as string; 
        row.address = reader[5] as string; 
        row.city = reader[3] as string; 
        row.region = reader[6] as string; 
        row.postalCode = reader[7] as string; 
        row.country = reader[8] as string; 
        row.phone = reader[9] as string; 
        row.fax = reader[10] as string; 
        Console.WriteLine("{0}\t{1}", reader[0], reader[1]); 

        Customers.Add(row); 
       } 

      } 
      reader.Close(); 
      connection.Close(); 

     } 

回答

2

我猜row是函數外定義一個全局變量。如果是這樣的話,你總是一遍又一遍地使用相同的行實例,因爲該行是一個引用類型。

定義while循環中的行,所以你的代碼應該成爲這樣的事情:

private void getData(string qry) 
    { 
     int count = 0; 
     string strProject = "ARTUR-PC\\SQLEXPRESS"; //Enter your SQL server instance name 
     string strDatabase = "Northwind"; //Enter your database name 
     string strUserID = "Artlemaks"; // Enter your SQL Server User Name 
     string strPassword = "rootUser"; // Enter your SQL Server Password 
     string strconn = "data source=" + strProject + 
     ";Persist Security Info=false;database=" + strDatabase + 
     ";user id=" + strUserID + ";password=" + 
     strPassword + ";Connection Timeout = 0"; 
     //conn = new SqlConnection(strconn); 
     using (SqlConnection connection = new SqlConnection(strconn)) 
     { 
      List<CustomerObj> Customers = new List<CustomerObj>(); 
      connection.Open(); 
      SqlCommand cmd = connection.CreateCommand(); 
      cmd.CommandText = qry; 

      cmd.ExecuteNonQuery(); 

      SqlDataReader reader = cmd.ExecuteReader(); 
      if (reader.HasRows) 
      { 
       while (reader.Read()) 
       { 
        CustomerObj row = new CustomerObj(); 
        row.custID = reader[0] as int?; 
        row.cmpName = reader[1] as string; 
        row.cntName = reader[2] as string; 
        row.cntTitle = reader[4] as string; 
        row.address = reader[5] as string; 
        row.city = reader[3] as string; 
        row.region = reader[6] as string; 
        row.postalCode = reader[7] as string; 
        row.country = reader[8] as string; 
        row.phone = reader[9] as string; 
        row.fax = reader[10] as string; 
        Console.WriteLine("{0}\t{1}", reader[0], reader[1]); 

        Customers.Add(row); 
       } 

      } 
      reader.Close(); 
      connection.Close(); 

     } 
+0

我抽了一口煙,得出了​​同樣的結論......完美地工作,謝謝你確認我的想法! – ArtleMaks

2

您正在使用相同的row對象實例比比皆是,因此它們的值僅在此對象實例設置。相反,您需要初始化類的新實例以在每次循環迭代中保存新結果。

要解決該問題,改變

while (reader.Read()) 
{ 
    row.custID = reader[0] as int?; 

while (reader.Read()) 
{ 
    CustomerObj row = new CustomerObj(); 
    row.custID = reader[0] as int?; 

,並從那裏聲明它刪除row聲明。