如何在简单的Web项目中连接到.mdf(Microsoft SQL Server数据库文件)?

时间:2021-09-15 00:01:14

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“数据库”)来连接到您可以拥有的数据源。看似简单,一旦你知道如何。

5 个解决方案

#1


20  

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 page applied 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;

#2


9  

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
  • 选择“Microsoft SQL Server数据库文件”,选择默认数据提供程序,然后单击“确定”
  • On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
  • 在下一个屏幕上,浏览到您的数据库文件,该文件将位于某个位置的VS Solution文件夹结构中。

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" />

#3


3  

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是唯一重要的部分)。打开文件,从“提供程序”选项卡开始,然后继续操作。一旦您对连接感到满意,请重命名该文件,为其提供.txt扩展名。打开文件并复制字符串 - 它相对简单,让您在使用之前测试连接。

#4


3  

<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

#5


0  

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();
}

#1


20  

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 page applied 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;

#2


9  

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
  • 选择“Microsoft SQL Server数据库文件”,选择默认数据提供程序,然后单击“确定”
  • On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
  • 在下一个屏幕上,浏览到您的数据库文件,该文件将位于某个位置的VS Solution文件夹结构中。

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" />

#3


3  

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是唯一重要的部分)。打开文件,从“提供程序”选项卡开始,然后继续操作。一旦您对连接感到满意,请重命名该文件,为其提供.txt扩展名。打开文件并复制字符串 - 它相对简单,让您在使用之前测试连接。

#4


3  

<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

#5


0  

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();
}