无法以第二种形式连接到数据库C# [英] Can't connect to database in second form C#

查看:60
本文介绍了无法以第二种形式连接到数据库C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am trying to connect database in project have multiple windows form connection to the first form to database is working fine but when is try to access database inside second form i get error

An attempt to attach an auto-named database for file c:\Users\username\AppData\Roaming\mydatabsename.mdf failed.A database with same name exist,or specified file cannot be opened,or it is located on UNC share.
Connection in first form is

My Connection string for first and second forms are  below

What I have tried:

<pre lang="C#">SqlConnection con = new SqlConnection(&quot;Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;&quot;); 
I use same connection in second form

SqlConnection con = new SqlConnection(&quot;Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;&quot;)
Please help me</pre>

推荐答案

不要使用AttachDbFilename来附加数据库:只能在Express版本中使用 - 因此通常不能在生产中使用 - 除非原始连接完成后将数据库分离,否则在出现错误时会出现错误你试着第二次附上它。这也是一个缓慢的连接,因为它启动SQL服务器的本地实例来处理单个用户会话中的请求。



基本上,让SQL处理数据库 - 每次要使用时都不要附上它。
Do not use AttachDbFilename to attach databases: that only works in Express editions - and thus doesn't often work in production - plus unless the original connection Detached the database when it was finished, you will get an error when you try to attach it a second time. It's also a slow connection, as it starts a "local instance" of SQL server to handle the request in a single user session.

Basically, let SQL handle the DB - don't attach it each time you want to use it.


这篇关于无法以第二种形式连接到数据库C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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