[英]How to use local database .sdf in c# wpf?
I am trying to make a database application. 我正在尝试创建一个数据库应用程序。 I added local database from 我从中添加了本地数据库
add > new item > local database.sdf
In Server Explorer, I created a table in the database. 在Server Explorer中,我在数据库中创建了一个表。 But I am having trouble connecting to it. 但我无法连接它。
I want to show all the data in a DataGrid. 我想在DataGrid中显示所有数据。
My code: 我的代码:
string ConnectionString = @"Data Source=""c:\users\asus\documents\visual studio 2012\Projects\WpfApplicationLocalDB\WpfApplicationLocalDB\LocalDB.sdf""";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", conn);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
List<DataRow> lis = dt.AsEnumerable().ToList();
DataGridView.ItemsSource = lis;
But when I build it, Visual Studio finds conn.open();
但是当我构建它时,Visual Studio会找到conn.open();
error. 错误。 A message says that 一条消息说
SqlException was unhandled by user 用户未处理SqlException
Please help... 请帮忙...
Also, can anyone suggest a tutorial of how can I create a simple database application in C#? 此外,任何人都可以建议如何在C#中创建一个简单的数据库应用程序的教程? Please help. 请帮忙。
If you're using a .sdf
file, you're using Microsoft SQL Server Compact Edition (SQL Server CE). 如果您使用的是.sdf
文件,那么您使用的是Microsoft SQL Server Compact Edition (SQL Server CE)。
When using SQL Server CE, you must use SqlCeConnection
and SqlCeCommand
classes - not SqlConnection
and SqlCommand
(those are for the "full", server-based versions of SQL Server) 使用SQL Server CE时,必须使用SqlCeConnection
和SqlCeCommand
类 - 而不是SqlConnection
和SqlCommand
(这些是用于“完整”,基于服务器的SQL Server版本)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.