How to use local database .sdf in c# wpf?

不打扰是莪最后的温柔 提交于 2019-12-10 18:17:54

问题


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. But I am having trouble connecting to it.

I want to show all the data in a 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(); error. A message says that

SqlException was unhandled by user

Please help...

Also, can anyone suggest a tutorial of how can I create a simple database application in C#? Please help.


回答1:


If you're using a .sdf file, you're using 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)



来源:https://stackoverflow.com/questions/20720110/how-to-use-local-database-sdf-in-c-sharp-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!