2016-04-16 77 views
0

我從我的SQLite數據庫中提取數據並在ListView中插入列表。但是我的ListView中正在重複相同的數據。SQLIte SELECT語句在Visual C中返回相同的值#

下面就來獲取數據的代碼:

public List<Objectdata> getAllData() 
    { 
     m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;"); 
     m_dbConnection.Open(); 
     List<Objectdata> dataList = new List<Objectdata>(); 
     Objectdata oD = new Objectdata(); 

     string sql = "select DISTINCT * from data ORDER BY id DESC"; 
     SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); 
     SQLiteDataReader reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      oD.id = reader["id"].ToString(); 
      oD.name = reader["name"].ToString(); 
      oD.address = reader["address"].ToString(); 
      oD.mobile = reader["mobile"].ToString(); 
      oD.date = reader["date"].ToString(); 
      oD.price = reader["price"].ToString(); 
      oD.warranty = reader["warranty"].ToString(); 
      oD.month = reader["month"].ToString(); 

      dataList.Add(oD); 

     } 

     m_dbConnection.Close(); 


     return dataList; 
    } 

這是將數據添加到ListView控件的代碼:

private void button_refresh_Click(object sender, RoutedEventArgs e) 
    { 
     listView.Items.Clear(); 
     TableController a = new TableController(); 
     dataList = a.getAllData(); 

     listView.ItemsSource = dataList; 


    } 

這是XAML的列表視圖代碼:

<ListView x:Name="listView" HorizontalAlignment="Left" Height="400" Margin="10,76,0,0" VerticalAlignment="Top" Width="645" > 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Id" Width="20" DisplayMemberBinding="{Binding id}" /> 
       <GridViewColumn Header="Name" Width="90" DisplayMemberBinding="{Binding name}" /> 
       <GridViewColumn Header="Adrress" Width="130" DisplayMemberBinding="{Binding address}" /> 
       <GridViewColumn Header="Mobile" Width="100" DisplayMemberBinding="{Binding mobile}" /> 
       <GridViewColumn Header="Date" Width="80" DisplayMemberBinding="{Binding date}" /> 
       <GridViewColumn Header="Price" Width="60" DisplayMemberBinding="{Binding price}" /> 
       <GridViewColumn Header="Warranty" Width="80" DisplayMemberBinding="{Binding warranty}" /> 
       <GridViewColumn Header="Month" Width="50" DisplayMemberBinding="{Binding month}" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 

幫手類:

class Objectdata 
{ 


    public String id 
    { 
     get; set; 
    } 

    public String name 
    { 
     get; set; 
    } 

    public String month 
    { 
     get; set; 
    } 

    public String mobile 
    { 
     get; set; 
    } 

    public String address 
    { 
     get; set; 
    } 

    public String date 
    { 
     get; set; 
    } 

    public String price 
    { 
     get; set; 
    } 

    public String warranty 
    { 
     get; set; 
    } 
} 

此輸出:

The Output

+0

日期如何存儲在數據庫中? –

+0

在簡單的日期合成MM/DD/YYYY和我用內置代碼計算月份。 –

回答

1

你永遠不會創建一個新的Objectdata元素,但總是在更新你的循環的相同。因此,列表中的所有元素都引用同一個對象。添加

oD = new Objectdata() 

在您的循環中將對象添加到列表中。或者將當前的oD聲明作爲循環體的第一條語句。無論如何,你似乎並沒有在循環之外使用它。

+0

現在正在工作。謝謝! –

+0

@basusingh不用客氣。請不要忘記投票並接受 – derpirscher