Visual C#中的数据库 [英] Database in visual c#

查看:94
本文介绍了Visual C#中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库项目有问题.
我已经用Visual C#开发了一个数据库软件.我已经使用了sql server数据库(.mdf).
问题是_
当我将整个软件转移到数据库的另一个目录连接是不可能的.发生错误.类似地,如果我将软件包转移到另一台PC,也会发生同样的问题.该程序仅在我开发它的目录中运行而没有错误.
我知道问题是由于连接字符串引起的.我已经编码了一个固定的连接字符串,但是当目录更改时,该字符串保持不变.我需要一种程序自动定位数据库的方式.我能做些什么?请帮忙.

I have a problem with my database project.
I have developed a database software in visual C#. I have used sql server database ( .mdf ).
The problem is _
when i shift my whole software to another directory connection to database is not possible.There occurs an error.Similarly if i move my software package to another pc the same problem occurs. The program only runs without error in the directory in which i developed it.
I know the problem is due to connection string. i have coded a fixed connection string but when directory changes the string remains unchanged. I need a way that the program automatic locate the database. What can i do? Please help.

推荐答案

如果您将数据库用作SQL Server,则连接字符串应独立于数据库位置-它应引用SQL Server实例,文件系统结构无法访问该文件.没有看到代码,尤其是连接字符串,很难确定,但是我猜想您的SQL连接字符串每次都在附加数据库,而不是让SQL Server处理数据库本身,并引用特定的通过连接字符串中的初始目录"(Initial Catalog)规范存储数据库.这有很多问题:数据库磁盘位置变得至关重要-如果您相对于数据库移动数据库或应用程序,则它很可能会失败,如您所见.这也意味着每个用户都有很大的机会使用自己的数据库副本,而不是在他们之间共享数据库.

让SQL Server处理数据库-并通过SQL实例而不是直接访问它.
If you have your database as SQL Server, then the connection string should be independent of the database location - it should refer to the SQL server instance, which is not accessed by a file system structure. Without seeing the code, and the connection string in particular, it is difficult to be sure, but I would guess that your SQL connection string is attaching the database each time, rather than letting SQL server handle the database itself, and referring to the specific database via the "Initial Catalog" specification in the connection string. This has a number of problems: the DB disk location become critical - if you move the DB or the app relative to the DB then it may well fail as you have seen. It also means that there is a good chance that each user will be using his own copy of the database instead of sharing the DB between them.

Let SQL server handle the database - and access it via the SQL instance instead of directly.


像这样在app.Config
中添加
add like this in app.Config
<add name="abc" connectionstring="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\abc.mdf;Integrated Security=True;User Instance=True" 

        providerName="System.Data.SqlClient" > 
</add>


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

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