如何使用C#WPF本地数据库。自卫队? [英] How to use local database .sdf in c# wpf?

查看:528
本文介绍了如何使用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. But I am having trouble connecting to it.

我要显示在DataGrid中的所有数据。

I want to show all the data in a DataGrid.

我的代码:

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;



但是,当我盖了,Visual Studio中找到 conn.open(); 错误。消息说,

SQLEXCEPTION是由用户未处理

SqlException was unhandled by user

请帮忙...

此外,任何人都可以建议我怎么可以创建在C#中一个简单的数据库应用程序的教程?请帮助。

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

推荐答案

如果您使用的是。自卫队文件,你使用Microsoft SQL Server的精简版(适用的SQL Server CE)。

If you're using a .sdf file, you're using Microsoft SQL Server Compact Edition (SQL Server CE).

在使用SQL Server CE,您必须使用的SqlCeConnection SqlCeCommand 类 - 不是的SqlConnection 的SqlCommand (这些都为满时,SQL Server基于服务器的版本)

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)

这篇关于如何使用C#WPF本地数据库。自卫队?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆