简体   繁体   English

如何从.NET应用程序连接到SQL Server Compact?

[英]How to connect to a SQL Server Compact from a .NET application?

I would like to create a WPF application that is using a local database. 我想创建一个使用本地数据库的WPF应用程序。 I have created a simple database and added it so I can see my database NameDB.sdf in my Solution Explorer. 我已经创建了一个简单的数据库并将其添加,以便可以在“解决方案资源管理器”中看到我的数据库NameDB.sdf

How do I connect to it from my application? 如何从我的应用程序连接到它?

I have tried with an empty application that just tries to connect to the database: 我尝试了一个空的应用程序,该应用程序仅尝试连接到数据库:

...
using System.Data.SqlServerCe;
using System.Data;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        SqlCeConnection connection =
            new SqlCeConnection(@"Data Source=|DataDirectory|\NameDB.sdf");
        SqlCeCommand command = new SqlCeCommand("SELECT * FROM Names");
        SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(command);
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
        Console.WriteLine("Done");
    }
}

But I get XamlParseException with this message: 但是我收到XamlParseException并显示以下消息:

'The invocation of the constructor on type 'WpfDBApp.MainWindow' that matches the specified binding constraints threw an exception.' '对与指定的绑定约束匹配的'WpfDBApp.MainWindow类型的构造函数的调用引发了异常。 Line number '3' and line position '9'. 行号“ 3”和行位置“ 9”。

My connection string is @"Data Source=|DataDirectory|\\NameDB.sdf" , since I want to use a "local" path and not a full path. 我的连接字符串为@"Data Source=|DataDirectory|\\NameDB.sdf" ,因为我想使用“本地”路径而不是完整路径。 This is taken from the tutorial I link to below. 这取自我链接到下面的教程。

How can I create a simple application that connects to the local database? 如何创建连接到本地数据库的简单应用程序?

I have tried to follow SQL Server 2005 Compact Edition Data Access with the SqlCeResultSet and Visual C#.NET but it doesn't seem to be done the same way with Visual Studio 2010 and WPF . 我已经尝试使用SqlCeResultSet和Visual C#.NET来遵循SQL Server 2005 Compact Edition数据访问,但是在Visual Studio 2010和WPF中似乎并没有以相同的方式进行。

You did not initialized your connection. 您尚未初始化连接。

SqlCeCommand command = new SqlCeCommand("SELECT * FROM Names", connection);

Try now. 现在试试。

The problem is your connection string. 问题是您的连接字符串。 The format should be "Data Source="{FullPath to YourDatabase.sdf}" 格式应为“ Data Source =“ {FullPath to YourDatabase.sdf}””

See this MSDN example for more details 有关更多详细信息,请参见此MSDN示例

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

相关问题 如何使用应用程序部署.Net Compact Framework和SQL Server Compact Edition - How to deploy the .Net Compact Framework and SQL Server Compact Edition with the Application 无法从C#连接到SQL Server Compact - Cannot connect to SQL Server Compact from C# 如何从在容器中运行的 ASP.NET 核心应用程序连接到具有集成安全性的 Windows 服务器上的 SQL 服务器 - How to connect from ASP.NET Core application running in a container to SQL Server on Windows Server with Integrated Security 如何创建SQL Server Compact 3.5 .sdf文件并连接到它? - How I create a SQL Server Compact 3.5 .sdf file and connect to it? 从asp.net应用程序连接到SQL Server数据库 - connect to sql server database from asp.net application 无法从.net核心应用程序连接到Sql Server - Cannot connect to Sql server from .net core application 从ASP.NET MVC应用程序连接到SQL Server - Connect to SQL Server from ASP.NET MVC application 如何在Sql Compact上连接表? - How to connect tables on Sql Compact? 从.net 4.5创建SQL Server Compact 3.5 - Create SQL Server Compact 3.5 from .net 4.5 如何通过Internet将.net应用程序连接到SQL Server - How to connect my .net application to SQL Server via internet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM