简体   繁体   English

如何在c#wpf中使用本地数据库.sdf?

[英]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时,必须使用SqlCeConnectionSqlCeCommand类 - 而不是SqlConnectionSqlCommand (这些是用于“完整”,基于服务器的SQL Server版本)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中连接到.sdf本地数据库文件 - How do I connect to a .sdf local database file in C# 如何在 C# 中使用本地数据库? - How to use a local database in c#? 使用Entity Framework和本地数据库文件(Sdf)生成C#整数主键 - C# integer primary key generation using Entity Framework with local database file (Sdf) 路径无效。 检查数据库目录。 -SDF文件在本地驱动器中-C# - The path is not valid. Check the directory for the database. - SDF file is in the local drive - C# 使用sdf数据库(SQL Server Compact 3.5数据库文件)而不安装Microsoft sql server C# - Use sdf database(SQL Server Compact 3.5 database file) without installing Microsoft sql server C# 使用本地数据库的WPF C#应用程序 - WPF C# application using local database C#:在安装项目中创建.exe时,如何包括.sdf数据库文件和Crystal Reports? - C# : how to include .sdf database file and Crystal Reports while creating .exe in setup project? DataSet更改为sdf本地数据库 - DataSet changes to sdf local database 使用C#将SQL Server数据库复制到.SDF文件 - Copy a SQL Server Database to .SDF file using C# 安装程序C#Windows Form App / SDF数据库 - Installer c# windows form app/sdf database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM