我從我的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;
}
}
此輸出:
日期如何存儲在數據庫中? –
在簡單的日期合成MM/DD/YYYY和我用內置代碼計算月份。 –