[英]C# WPF DataGrid connecting to a SQLite database
I'm new to C# and WPF so I would appreciate your help.我是 C# 和 WPF 的新手,因此非常感谢您的帮助。
I can't get a WPF grid to connect to a SQLite database and display its data.我无法让 WPF 网格连接到 SQLite 数据库并显示其数据。 I just get the Grid displayed without any data.
我只是在没有任何数据的情况下显示网格。
The Datagrid code on Home.xaml: Home.xaml 上的 Datagrid 代码:
<DataGrid Name="DataGridHome" Margin="10,66,242,185" Grid.Row="2" Grid.Column="0" AutoGenerateColumns="False" CanUserAddRows="False"
CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False"
CanUserResizeRows="False" CanUserSortColumns="False" SelectionMode="Single" SelectionChanged="DataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Visibility="Hidden"/>
<DataGridTextColumn Header="Surname" FontFamily="Arial"/>
<DataGridTextColumn Header="First Name" FontFamily="Arial" />
<DataGridTextColumn Header="DoorNum" FontFamily="Arial"/>
<DataGridTextColumn Header="StreetName" FontFamily="Arial"/>
<DataGridTextColumn Header="StreetName2" FontFamily="Arial"/>
<DataGridTextColumn Header="City" FontFamily="Arial"/>
<DataGridTextColumn Header="PostCode" FontFamily="Arial"/>
<DataGridTextColumn Header="Belt" FontFamily="Arial"/>
<DataGridTextColumn Header="AccountType" FontFamily="Arial"/>
<DataGridTextColumn Header="JoinDate" FontFamily="Arial"/>
<DataGridTextColumn Header="MembershipActive" FontFamily="Arial"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
The App.config code (my database doesn't have any username or password): App.config 代码(我的数据库没有任何用户名或密码):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add connectionString="Data Source=MemberDB.db; Initial Catalog=Pubs;" name="ConString"/>
</connectionStrings>
</configuration>
The C# code Home.xaml.cs : C# 代码 Home.xaml.cs :
using System;
using System.Windows;
using System.Windows.Controls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.IO;
namespace KarateClub
{
/// <summary>
/// Interaction logic for Home.xaml
/// </summary>
public partial class Home : Page
{
public Home()
{
InitializeComponent();
DataBaseConnection();
}
private void DataBaseConnection()
{
try
{
SQLiteConnection sqlite = new SQLiteConnection("Data Source=MemberDB.db; Version=3;");
SQLiteCommand sqlcmd;
sqlite.Open();
sqlcmd = sqlite.CreateCommand();
string query = "SELECT * FROM Members";
sqlcmd.CommandText = query;
SQLiteDataAdapter sda = new SQLiteDataAdapter(sqlcmd);
DataTable dt = new DataTable("Members");
sda.Fill(dt);
DataGridHome.ItemsSource = dt.DefaultView;
sqlite.Close();
}
catch(SQLiteException ex)
{
Console.WriteLine("SQLite DataBase Error!");
}
}
....
I'm thinking there must be something wrong with App.config or my connection to the database in the C# code.我认为 App.config 或我在 C# 代码中与数据库的连接一定有问题。 I don't know how name="ConString" in App.config is supposed to be used with the C# code to access SQLite.
我不知道 App.config 中的 name="ConString" 应该如何与 C# 代码一起使用来访问 SQLite。
This has been VERY hard to research online - some people using Dapper, others using ADO.NET, the ones who have similar code to me are not using a DataGrid but a simple list view.这在网上很难研究——有些人使用 Dapper,其他人使用 ADO.NET,那些与我有类似代码的人不是使用 DataGrid,而是使用简单的列表视图。
Thanks :)谢谢 :)
You can try giving full file path in Data Source .您可以尝试在Data Source 中提供完整的文件路径。 By debugging, verify that the sda variable is having required values from database.
通过调试,验证sda变量是否具有来自数据库的所需值。
您可以在此处找到有效的 SQL Lite 连接字符串列表。
A datagrid connects to a list -> This is what you need to do:数据网格连接到列表 -> 这是您需要做的:
A good tutorial: https://www.c-sharpcorner.com/UploadFile/009464/how-to-bind-datagrid-in-wpf-using-C-Sharp/一个很好的教程: https : //www.c-sharpcorner.com/UploadFile/009464/how-to-bind-datagrid-in-wpf-using-C-Sharp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.