简体   繁体   English

WPF连接到SQL Server Compact数据库

[英]WPF connection to a SQL Server Compact database

Basically what I am trying to do is run a small WPF program, where the user will select a SQL Server Compact database and my program will show them specific information from a specific table. 基本上我想做的是运行一个小的WPF程序,用户将在其中选择一个SQL Server Compact数据库,而我的程序将向他们显示来自特定表的特定信息。

Where I am having trouble is the connection string, I want the user to be able to browse to the location where their database is saved, and not make them have it in the one location (if that makes sense) 我遇到麻烦的地方是连接字符串,我希望用户能够浏览到保存数据库的位置,而不是让他们将其放在一个位置(如果这样的话)

I can get my program to work if I have the database in one location with a specific name, but the databases the user will want to check the information for will all have different names but all will have the same file extension 如果我将数据库放在一个具有特定名称的位置中,则可以使我的程序正常工作,但是用户要检查其信息的数据库都将具有不同的名称,但都具有相同的文件扩展名

eg the code I have at the moment is this: 例如我现在的代码是这样的:

string Myfile = @"C:\Users\documents\visual studio 2010\debug\FILE.sdf";
SqlCeConnection localDatabaseConn = new SqlCeConnection("data base = " + Myfile +";

I can get the result I want when I have the file saved to a specific location with a specific name, but I want the user to be able to browse to their own file, which will have a different name and could be anywhere on their machine. 将文件保存到具有特定名称的特定位置时,可以获得我想要的结果,但是我希望用户能够浏览到自己的文件,该文件将具有不同的名称,并且可以在其计算机上的任何位置。

Any help would be appreciated 任何帮助,将不胜感激

So, basically it seems you want an OpenFileDialog . 因此,基本上看来您想要一个OpenFileDialog

Example: 例:

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".sdf";
dlg.Filter = "Database file (.sdf)|*.sdf";

Nullable<bool> result = dlg.ShowDialog();

if (result == true)
{
    string Myfile = dlg.FileName;
}

WPF has some built-in file dialogs. WPF具有一些内置的文件对话框。 The one you're looking for probably is Microsoft.Win32.OpenFileDialog 您正在寻找的可能是Microsoft.Win32.OpenFileDialog

Usage: 用法:

var dlg = new Microsoft.Win32.OpenFileDialog();

if (dlg.ShowDialog();)
{
    string filename = dlg.FileName;
}

You have to use OpenFileDialog . 您必须使用OpenFileDialog and your connection string must be "Data Source="+YouPath and No the "Data Base"+YourPath 并且您的连接字符串必须是“ Data Source =“ + YouPath,并且没有“ Data Base” + YourPath

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM