【发布时间】:2015-03-19 21:58:25
【问题描述】:
我有一个使用 LINQ 连接到 SQL-Server Express 数据库的 WPF 应用程序。我以前很少使用数据库,我正在尝试解决这个问题,所以如果我使用了错误的术语,请随时纠正我。
我的目标是发布程序并让它复制数据库,以便安装它的人可以查看信息并在他们的计算机或其他任何地方进行编辑。
从这张表来看,我认为对于视觉工作室来说,点击一次是可能的:http://erikej.blogspot.co.uk/2011/01/comparison-of-sql-server-compact-4-and.html
如果我部署该程序,我当然可以将它安装在我自己的计算机上,但是如果我尝试将它安装在另一台计算机上,我会收到错误:
Application download did not succeed. Check your network connections...
详细说明:
Activation of C:\myapp.application resulted in exception.
+ Downloading http://mycomputername/myapp.application did not succeed
但我不确定它为什么要尝试从 Internet 下载任何内容,因为我将其发布为从 CD-ROM/DVD-ROM 安装。
所以我做了一些研究并将数据库 .dbml Build Action 更改为 Content 并将 Copy to Output Directory 更改为 Copy if Newer 并尝试了这些设置,但这并没有解决我的问题。
我还在 MyProject -> Properties -> Settings 中看到我的连接字符串是这样定义的:
我应该如何更改它以便可以使用程序部署数据库?我想我应该以.\SQLEXPRESS 开头,但这也没有解决我的问题。
我找到了其他论坛,但他们通常在代码中手动连接到数据库,而 LINQ 为我生成如下代码,因此似乎不适用。
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MyDataBase")]
public partial class CustomersDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
public CustomersDataContext() :
base(global::myApp.Properties.Settings.Default.myConnectionString, mappingSource)
{
OnCreated();
}
/* more stuff like that */
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.MyTable")]
public partial class MyClass
{
/* column definitions */
我的应用程序面向 .NET 框架 4.5,我使用的是 Visual Studios Professional 2013。
我如何告诉 Visual Studios 将数据库与应用程序捆绑在一起并在安装时进行部署?
非常感谢您的帮助。
编辑:
现在要将我的程序连接到数据库,我只需从 SQL Server 对象资源管理器中将数据库拖入即可。
然后可以像这样轻松加载它:
CustomersDataContext cd = new CustomersDataContext();
var customers = (from p in cd.MyDataBase
select p).ToList();
MyDataGrid.ItemsSource = customers;
并像这样轻松地编辑它:
CustomersDataContext dataContext = new CustomersDataContext();
MyClass newRow = MyDataGrid.SelectedItem as MyClass;
MyClass row = (from p in dataContext.MyDataBase
where p.ID == customerRow.ID
select p).Single();
row = newRow;
dataContext.SubmitChanges();
我真的更喜欢不会改变我与数据库交互方式的东西,因为这会让我重新构建整个东西。
【问题讨论】:
-
在这种情况下我使用 SQLite,它很容易设置和使用。 nickcharlton.net/posts/sqlite-with-csharp.html
-
@doro 有趣,感谢您的建议。目标计算机是否需要 SQLite 才能安装程序?
-
不,这就是重点。您只需要在开发人员计算机上安装 sqlite。一开始我对使用 sqlite 不满意(这是项目要求),但后来我发现它是一个很好的解决方案,只要它用作小型本地数据库。但是,如果需要插入大量数据,则应该使用事务,否则插入会很慢。
-
如果我可以这样做:stackoverflow.com/a/8495991/2374028 我更愿意这样做,然后直接使用 SQLite 编辑代码。如果那不可能,我会听取您的建议并可能会遵循:codeproject.com/Articles/153407/WPF-and-SQLite-Database
-
我不太明白你的问题,这很容易实现。我通常使用 ado.net 访问 sqlite。你能给我一些你的代码示例吗?
标签: c# sql-server wpf linq visual-studio