ASP.NET网页连接到SQL数据库 [英] Connect ASP.NET WebSite to SQL Database
问题描述
目前我正在试图建立一个ASP.NET网站项目和一个数据库SQL Server 2008所内置的连接R2。
我必须这样做的方法是使用的connectionString
从的Web.config
页,但我不知道什么价值给它或如何建立使用所述值的连接。 (使用C#)
任何帮助将是AP preciated,因为我发现旁边没有关于这方面的信息。
下面是(默认)值,目前在的Web.config
页:
<的ConnectionStrings>
<添加名称=ApplicationServices的connectionString =数据源= \ SQLEX $ P $干燥综合征;集成安全性= SSPI; AttachDBFilename = | DataDirectory目录| \ ASPNETDB.MDF;用户实例=真的providerName =System.Data.SqlClient的/>
< /的ConnectionStrings>
使用配置管理器:
使用System.Data.SqlClient的;
使用System.Configuration;
字符串的connectionString = ConfigurationManager.ConnectionStrings [ApplicationServices]的ConnectionString。
使用(SqlConnection的SqlConnection的=新的SqlConnection(的connectionString));
//剩下的就是这里向您展示如何连接将被使用。但是,code以上这条评论是你真正问,这是如何连接。
{
SqlDataAdapter的SqlDataAdapter的=新的SqlDataAdapter();
的SqlCommand的SqlCommand =新的SqlCommand();
在SQLConnection.open();
SqlCommand.CommandText =选择表*;
SqlCommand.Connection = SqlConnection的;
SqlDataReader的博士= SqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
I am currently trying to establish a connection between an ASP.NET web site project and a Database built by SQL Server 2008 R2.
The way I am required to do so is to use the connectionString
from the Web.config
page, but I have no idea what value to give it or how to establish a connection using said value. (Using C#)
Any help would be appreciated, as I found next to no information about the subject.
Here is the (default) value that is currently in the Web.config
page:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Use Configuration Manager:
using System.Data.SqlClient;
using System.Configuration;
string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
using(SqlConnection SqlConnection = new SqlConnection(connectionString));
//The rest is here to show you how this connection would be used. But the code above this comment is all you really asked for, which is how to connect.
{
SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();
SqlCommand SqlCommand = new SqlCommand();
SqlConnection.Open();
SqlCommand.CommandText = "select * from table";
SqlCommand.Connection = SqlConnection;
SqlDataReader dr = SqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
这篇关于ASP.NET网页连接到SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!