附加数据库文件,字符串 [英] Attach Database file, conncetion String

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

问题描述

我在Visul studio-2008的C#.net中有一个项目,在SQL Server-2008中的数据库中有一个项目.这是Windows应用程序项目.

我在Debug文件夹中创建了一个名为"Data"的文件夹. &我将数据库.mdf和ldf文件复制并粘贴到"Data"文件夹中

现在我要在数据"文件夹中连接数据库. 我有一个用于连接字符串的代码
"
sqlcon =新的SqlConnection(@"Data Source =".\ SQLExpress;初始目录=;集成安全性= true; AttachDBFileName ="''+ Application.StartupPath + @"\ Data \ Laboratory1.mdf; Uid = sa; Pwd = zxc'');
"
但它不起作用.执行时出现错误;

所以我想知道
将数据库副本粘贴到调试文件夹中的数据文件夹"中时,连接字符串是什么?

请帮助我.

I have a project in C#.net ,Visul studio-2008 with database in SQL Server-2008.It is windows application project.

I have create a folder named ''Data'' in Debug folder. & I copy and paste data base .mdf and ldf file into ''Data'' folder

Now i want to connect databse in ''Data'' Folder .
I have a code for connection string
"
sqlcon = new SqlConnection(@"Data Source=''.\SQLExpress''; Initial Catalog=; Integrated Security=true; AttachDBFileName=''" +Application.StartupPath+@"\Data\Laboratory1.mdf; Uid=sa;Pwd=zxc''");
"
but its not working.I got an error at execution time;

So i want to know
what is the connection string when the database copy paste in Data Folder with in debug folder?

Please help me.

推荐答案

亲爱的您应该拥有Sql sever express来运行这些服务器.
然后在App_code中创建数据库.

您的参考连接字符串将为-

Dear you should have Sql sever express to run these .
then create your database in App_code .

for your reference connection string will be -

<connectionStrings>
      <add name="DatabaseConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=true" providerName="System.Data.SqlClient"/>
   </connectionStrings>



对于Windows,您可以使用此



for windows you can use this

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;


严重吗?我不会这样做,因为它依赖最终的生产程序具有在该文件夹上写入文件的权限,该文件夹位于"Program Files"文件夹下,该文件夹通常仅限于Admin.您可能会在这里遇到相同的权限问题.

相反,请将您的数据库放在更易于访问的位置-参见此处:我应该在哪里存储我的数据? [ ^ ]
Seriously? I wouldn''t do that, as it relies on the eventual production program having permission to write files on the folder, which will be below the "Program Files" folder, which is normally restricted to Admin only. You will probably be getting the same permissions problem here.

Instead, keep your database in a more accessible place - see here: Where should I store my data?[^]


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

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