ASP.NET连接数据库配置文件
// 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连接数据库配置文件的更多相关文章
- ASP.NET连接数据库时,提示“用户 'sa' 登录失败原因: 未与信任 SQL Server 连接相关联
用ASP.NET连接数据库时,提示"用户 'sa' 登录失败.原因: 未与信任 SQL Server 连接相关联.".解决方法:首先检查是不是web.config文件内的用户名密码 ...
- asp.net连接数据库
Asp.net web连接数据库步骤. 一. 新建一个web工程. 1.文件->添加->新建网站->asp.net web网站Winform窗体. 2.新建好的网站最下面有 ...
- asp.net读写配置文件方法
方法1: System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameVa ...
- 【转】asp.net连接数据库字符串有哪些写法[数据连接]
来源:http://blog.unvs.cn/archives/database-link-method.html 下面是个人总结的net网站连接数据库的字符串几种写法: 一.结合webconfig位 ...
- asp.net core配置文件
读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettings>节点.它的数据格式变成了j ...
- asp.net在配置文件里设置多种编码方式的研究
我们在做asp.net的程序时,在根目录下肯定会有一个web.config的文件, 有点开发经验的可能都知道,它是配置程序的全局信息的地方, 当然了,也可以在这里做更多的事情,下面我们来研究一下 ,如 ...
- asp.net 读取配置文件方法
方法1: System.Collections.Specialized.NameValueCollection nvc = (System.Collections.Specialized.NameVa ...
- ASP.NET Core 配置文件(无处不在的依赖注入)
前烟: .NET Core 中取消了以往的 XML 节点配置文件,改用了 *.json 格式. 在 Startup.cs 文件中,构造方法 build appsetting.json 文件, 本文主要 ...
- ASP.NET Core 配置文件
在ASP.NET Core 中,应用程序配置数据可以使用JSON, XML 和 INI格式 和内置环境变量,命令行参数或内存中的集合. 1.如何获取和设置配置 ASP.NET Core配置系统针对以前 ...
随机推荐
- INC函数
1.inc(integer) inc(a)等于a:=a+1; 例: i := 100; inc(i); 结果:i等于101 2.inc(integer,integer) inc(a,b)等于a:=a+ ...
- NavigationControllerr滑动返回
iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势.支持此手势的是UINavigationController中新增的属性 interactivePopGestureRecogniz ...
- 【uoj57】 WC2013—平面图
http://uoj.ac/problem/57 (题目链接) 题意 给出二位平面上n个点,点之间有一些连线,连线不在顶点之外的地方相交,将平面分为若干个区域.给出一些询问点对,问从这个点所在的区域走 ...
- Unity3D ——强大的跨平台3D游戏开发工具(六)
第十一章 制作炮台的旋转 大家知道,炮台需要向四周不同的角度发射炮弹,这就需要我们将炮台设置成为会旋转的物体,接下来我们就一起制作一个会旋转的炮台. 第一步:给炮台的炮筒添加旋转函数. 给炮台的炮筒部 ...
- git 关联远程库(https协议)
1.在oschina上新建库 2.在本地文件夹右键->"git Bash here" 3.设置全局变量: git config --global user.name &quo ...
- UVa 10700 - Camel trading
题目大意:给一个不含括号.只有+和*运算的表达式,数字的范围在1到20之间,算出计算结果的可能最大值和最小值. 贪心,如果加法优先级比乘法高,那么得出的结果为最大值.(a+b)*c = a*c + b ...
- Swift分割字符串
var str_componets = "I Like Swift " str_componets.componentsSeparatedByString(" " ...
- 升级wamp5集成安装包 php5.2到php5.3
平时xp下面都使用wamp5集成开发 但php的空间命名需要php5.3 才支持,而且公司系统大部分都使用5.3,很多函数与5.2是不同的 难的在xp下面手动安装,集成包使用很方便,配置,快捷键都很不 ...
- 判断js对象的数据类型,有没有一个最完美的方法?
先来一个例子: var string1=""; var string2=new String(""); alert(typeof string1); // st ...
- Angular - - $location 和 $window
$location $location服务解析浏览器地址中的url(基于window.location)并且使url在应用程序中可用.将地址栏中的网址的变化反映到$location服务和$locati ...