将数据库附加到我的项目 [英] Attaching database to my project

查看:95
本文介绍了将数据库附加到我的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#程序中我有一个大问题:

In my C# program I have a big problem:

我添加一个名为Behgozin_DB的SQL服务器数据库到我的项目。我按照以下步骤添加数据库:

I add an SQL server database named "Behgozin_DB" to my project. I do the following steps for adding the database:


  1. 在我的SQL服务器管理工​​作室中,我分离了我的数据库

  2. 在我的视觉工作室中,从数据菜单添加新数据源

  3. 我的连接字符串是:


$ b b


数据源=(local); AttachDbFilename = | DataDirectory | \DB_Behgozin.mdf;集成安全性= True

Data Source=(local);AttachDbFilename=|DataDirectory|\DB_Behgozin.mdf;Integrated Security=True

一切正常,但当我关闭应用程序后,我插入一些东西到我的数据库,我看不到任何数据。但是当我右键单击我的视觉工作室服务器浏览器菜单中的表,并选择显示表数据,我的应用程序完全读取整个数据表!

Everything is OK but when I insert something into my database after closing the application , I can not see any data. But when I right click on my tables in my visual studio server explorer menu and choose show table data, my application reads the whole table of data completely!

另一个问题是使用此应用程序,即使它不能读取其自己的数据在表中。

Another problem is after working with this app, even it can not read its own data in table.

它是Visual Studio 2010 SQL Server 2008中的一个C#项目。

Its a C# project in Visual Studio 2010 SQL Server 2008.

推荐答案

您的数据库文件有2个副本。一个在您的项目中,另一个在 bin\debug 中。在设计时,编辑项目中的一个。每次您的项目更改并构建它时,项目数据库文件都将覆盖bin \debug中的文件,并且此文件将在运行时更新。

Your database file has 2 copies. One is in your project and the other is in bin\debug. In design time you edit the one in your project. Each time your project changes and you build it, the project database file is overwriting the file in bin\debug, and this file you're updating in run-time.

为了解决你有三个选项:

In order to solve that you have three options:


  1. 只使用bin \debug中的文件。将项目数据库文件复制选项设置为从不(在属性窗口中),显示项目的所有文件,并将bin \debug数据库文件添加到服务器浏览器。注意:如果您不小心删除了bin \debug文件夹,则所有更改都已删除。

  1. Use only the file in bin\debug. Set the project database file copy options to "Never" (in the properties window), show all files of the project and add the bin\debug database file to the server explorer. Caution: If you accidently delete the bin\debug folder all your changes are gone. Manually copy the database file from bin\debug to the project folder when you change it.

将文件放在您的sql server文件夹中,而不是您的项目中。将文件从bin \debug手动复制到项目文件夹。 。

Put the file in your sql server folder and not in your project. When deploying, change the connection string and copy the database from server.

将| DataDirectory |更改为连接字符串并从服务器复制数据库。宏与项目数据库文件的绝对路径。部署时,只需将其更改回| DataDirectory |

Replace "|DataDirectory|" macro with the absolute path to the project database file. When deploying, just change it back to "|DataDirectory|"

这篇关于将数据库附加到我的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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