码迷,mamicode.com
首页 > 数据库 > 详细

ASP.NET连接数据库配置文件

时间:2015-10-13 09:12:16      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

// Web.Config
<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <system.web>
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <appSettings>
    <add key="connectionString" value="server=.;database=stuTest;    uid=sa;pwd=123456"/>
  </appSettings>
</configuration>


// 获取配置文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class GridViewPage_One : System.Web.UI.Page
{
    //第一种写法已经过时,会提示错误信息将ConfigurationSettings改成ConfigurationManager
   //然后在命名空间中加入using System.Configuration; // private string connectionString = ConfigurationSettings.AppSettings["connectionString"].ToString(); private string connectionString = ConfigurationManager.AppSettings["connectionString"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { FillList(); } } private void FillList() { string selectSql = "select * from Stu"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(selectSql,con); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); try { con.Open(); adapter.Fill(ds,"Students"); this.GridView1.DataSource = ds.Tables["Students"].DefaultView; this.GridView1.DataBind(); } catch(Exception err) { throw err; } } }

  

ASP.NET连接数据库配置文件

标签:

原文地址:http://www.cnblogs.com/IamJiangXiaoKun/p/4873446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!