// 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连接数据库配置文件的更多相关文章

  1. ASP.NET连接数据库时,提示“用户 'sa' 登录失败原因: 未与信任 SQL Server 连接相关联

    用ASP.NET连接数据库时,提示"用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联.".解决方法:首先检查是不是web.config文件内的用户名密码 ...

  2. asp.net连接数据库

    Asp.net web连接数据库步骤. 一.      新建一个web工程. 1.文件->添加->新建网站->asp.net web网站Winform窗体. 2.新建好的网站最下面有 ...

  3. asp.net读写配置文件方法

    方法1: System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameVa ...

  4. 【转】asp.net连接数据库字符串有哪些写法[数据连接]

    来源:http://blog.unvs.cn/archives/database-link-method.html 下面是个人总结的net网站连接数据库的字符串几种写法: 一.结合webconfig位 ...

  5. asp.net core配置文件

    读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettings>节点.它的数据格式变成了j ...

  6. asp.net在配置文件里设置多种编码方式的研究

    我们在做asp.net的程序时,在根目录下肯定会有一个web.config的文件, 有点开发经验的可能都知道,它是配置程序的全局信息的地方, 当然了,也可以在这里做更多的事情,下面我们来研究一下 ,如 ...

  7. asp.net 读取配置文件方法

    方法1: System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameVa ...

  8. ASP.NET Core 配置文件(无处不在的依赖注入)

    前烟: .NET Core 中取消了以往的 XML 节点配置文件,改用了 *.json 格式. 在 Startup.cs 文件中,构造方法 build appsetting.json 文件, 本文主要 ...

  9. ASP.NET Core 配置文件

    在ASP.NET Core 中,应用程序配置数据可以使用JSON, XML 和 INI格式 和内置环境变量,命令行参数或内存中的集合. 1.如何获取和设置配置 ASP.NET Core配置系统针对以前 ...

随机推荐

  1. angular中ui calendar的一些使用心得

    ui calendar是封装fullcalendar的一款angular指令插件 官方地址:http://angular-ui.github.io/ui-calendar/ fullcalendar ...

  2. php 中文切割字符串长度

    function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_ex ...

  3. sqlserver递归查询

    --递归查询 with cte as ( ' union all select k.id,k.Text, k.name,k.pid from Menu k inner join cte c on c. ...

  4. iOS真机调试配置

    啊!生活不易啊~~~~据说这个过程都可以当做简历技能了... 准备:已经具备了企业开发者账号,和相关证书 目标:让爪机可以真机调试 过程:1.登录官方开发网站 https://developer.ap ...

  5. OI队内测试一【数论概率期望】

    版权声明:未经本人允许,擅自转载,一旦发现将严肃处理,情节严重者,将追究法律责任! 序:代码部分待更[因为在家写博客,代码保存在机房] 测试分数:110 本应分数:160 改完分数:200 T1: 题 ...

  6. form 表单 设置编码和页面编码

    <HTML> <HEAD> <meta http-equiv=content-type content="text/html; charset=UTF-8&qu ...

  7. ipyparallel 中的 pi的求法

    1.PI的求法的数学依据 如图,可以看见在边长为1的正方形里面,有一个1/4圆,我们随机在正方形中取点,点在圆内的概率和点在正方形内的概率之比正好为两者的面积之比.这样就有在圆内的点的数目比所有点的数 ...

  8. Spark中的键值对操作-scala

    1.PairRDD介绍     Spark为包含键值对类型的RDD提供了一些专有的操作.这些RDD被称为PairRDD.PairRDD提供了并行操作各个键或跨节点重新进行数据分组的操作接口.例如,Pa ...

  9. Add Strings Leetcode

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  10. Eclipse 打开文件所在文件夹

    右击文件 > Show In > System Explorer