C# 如何在简单的 Web 项目中连接到 .mdf(Microsoft SQL Server 数据库文件)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/173209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project?
提问by MrBoJangles
Specifically, in VS 2008, I want to connect to a data source that you can have by right-clicking on the automatically-generated App_Data folder (an .mdf "database"). Seems easy, and it is once you know how.
具体来说,在 VS 2008 中,我想连接到您可以通过右键单击自动生成的 App_Data 文件夹(.mdf“数据库”)来拥有的数据源。看起来很容易,一旦你知道怎么做。
回答by MrBoJangles
So here's the answer from MSDN:
所以这是来自 MSDN 的答案:
Choos[e] "Add New Data Source" from the Data menu.[And follow the connection wizard]
从数据菜单中选择[e]“添加新数据源”。[并按照连接向导]
Very easy, except that I have no Data menu. If you don't have a Data menu, do the following:
很简单,只是我没有数据菜单。如果您没有“数据”菜单,请执行以下操作:
- Click on Tools >> Connect to Database...
- Select "Microsoft SQL Server Database File", take the default Data provider, and click OK
- On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
- 单击工具>>连接到数据库...
- 选择“Microsoft SQL Server 数据库文件”,采用默认数据提供程序,然后单击“确定”
- 在下一个屏幕上,浏览到您的数据库文件,该文件将位于您的 VS 解决方案文件夹结构中的某处。
Test the connection. It'll be good. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the "AttachDbFilename
" attribute and value. Example:
测试连接。会好的。如果要将字符串添加到 web.config,请单击“高级”按钮,然后复制“数据源”行(位于对话框底部),并将其粘贴到 web.config 中适当位置的连接字符串中文件。您必须添加 " AttachDbFilename
" 属性和值。例子:
The raw text from the Advanced panel:
高级面板中的原始文本:
Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True
The actual entry in the web.config:
web.config 中的实际条目:
<add name="SomeDataBase" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Development\blahBlah\App_Data\SomeDataFile.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />
回答by WebDude
A great resource I always keep around is connectionstrings.com. It's really handy for finding these connection strings when you can't find an example.
我一直保留的一个很好的资源是connectionstrings.com。当你找不到一个例子时,找到这些连接字符串真的很方便。
Particularly this pageapplied to your problem
特别是此页面适用于您的问题
Attach a database file on connect to a local SQL Server Express instance
在连接到本地 SQL Server Express 实例时附加数据库文件
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
回答by WebDude
Just one more -- i've always kept a udl file on my desktop to easily create and test connection strings. If you've never done it before - create a new text file and name it to connection.udl (the ext is the only important part). Open the file, start on the Provider tab and work your way through. Once you're happy with the connection rename the file giving it a .txt extension. Open the file and copy the string - it's relatively easy and lets you test the connection before using it.
还有一个——我总是在我的桌面上保存一个 udl 文件,以便轻松创建和测试连接字符串。如果您以前从未这样做过 - 创建一个新的文本文件并将其命名为 connection.udl(ext 是唯一重要的部分)。打开文件,从 Provider 选项卡开始,然后按自己的方式工作。对连接感到满意后,重命名文件并为其添加 .txt 扩展名。打开文件并复制字符串 - 这相对容易,并且可以让您在使用之前测试连接。
回答by Spice
<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
回答by Sudheesh
In your Login.aspx.cs (the code behind file for your login page in the submit button click event) add
在您的 Login.aspx.cs(提交按钮单击事件中登录页面的代码隐藏文件)中添加
string constr = @"Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\myData.mdf; Integrated Security=True; Connect Timeout=30;";
using (SqlConnection conn = new SqlConnection(constr))
string constr = ConfigurationManager.ConnectionStrings["myData"].ToString();
using (SqlConnection conn = new SqlConnection(constr))
{
sqlQuery=" Your Query here"
SqlCommand com = new SqlCommand(sqlQuery, conn);
com.Connection.Open();
string strOutput = (string)com.ExecuteScalar();
}