qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

web.config配置文件链接数据库的方法

  【1】在web.config配置文件中,
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
  【2】在后台中,引入程序集或者手工引入:using System.Web.Configuration;
  【3】后台写入的方法为:
protected void Page_Load(object sender, EventArgs e)
{
//从web.config中引用连接字符串
string strConn = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection myConn = new SqlConnection(strConn);
string strSQL = "SELECT Top 5 CategoryID,CategoryName,Description From Categories";
SqlCommand myCommand = new SqlCommand(strSQL, myConn);
myConn.Open();
myGv.DataSource = myCommand.ExecuteReader();
myGv.DataBind();
myConn.Close();
}

posted on 2013-10-29 10:40 顺其自然EVO 阅读(390) 评论(0)  编辑  收藏 所属分类: 数据库


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年10月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜