ASP.NET数据访问 - 四大对象

简介: 今天总结下ASP.NET中的基本数据访问。写过ASP数据库编程的朋友应该知道,在ASP中访问数据库主要用到三大对象:Connection, Command, RecordSet新一代的ADO.NET对老的ADO进行了升级,主要有四大对象:1)SqlConnection2)SqlCommand3)SqlDataAdapter4)DataSet其中,SqlDataAdapter是新增加的适配器对象。
今天总结下ASP.NET中的基本数据访问。
写过ASP数据库编程的朋友应该知道,在ASP中访问数据库主要用到三大对象:
Connection, Command, RecordSet

新一代的ADO.NET对老的ADO进行了升级,主要有四大对象:
1)SqlConnection
2)SqlCommand
3)SqlDataAdapter
4)DataSet


其中,SqlDataAdapter是新增加的 适配器对象。
它用来 填充结果集。

1)建立并打开连接
2)根据连接和sql语句创建适配器
3)用适配器填充结果集
4)数据绑定-将结果
集绑定到控件

以北风数据库为例,具体来举个例子:
 ASPX代码:
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " dataAccess1.aspx.cs "  Inherits = " BlogNet.ASPXDemo.dataAccess1 "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > ASP.NET数据访问-四大对象 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
    
    
< asp:GridView  ID ="GridView1"  
        runat
="server"  
        AutoGenerateColumns
="False"
        AllowPaging
="True"  
        AllowSorting
="True"  
        PageSize
="20"  
        OnPageIndexChanging
="GridView1_PageIndexChanging" >
        
< Columns >
            
< asp:BoundField  DataField ="CustomerID"  HeaderText ="CustomerID"  ReadOnly ="True"  
                SortExpression
="CustomerID"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="CompanyName"  HeaderText ="CompanyName"  
                SortExpression
="CompanyName"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="ContactName"  HeaderText ="ContactName"  
                SortExpression
="ContactName"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="ContactTitle"  HeaderText ="ContactTitle"  
                SortExpression
="ContactTitle"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="Address"  HeaderText ="Address"  
                SortExpression
="Address"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="City"  HeaderText ="City"  SortExpression ="City"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="Region"  HeaderText ="Region"  
                SortExpression
="Region"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="PostalCode"  HeaderText ="PostalCode"  
                SortExpression
="PostalCode"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="Country"  HeaderText ="Country"  
                SortExpression
="Country"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="Phone"  HeaderText ="Phone"  SortExpression ="Phone"  NullDisplayText ="N/A"   />
            
< asp:BoundField  DataField ="Fax"  HeaderText ="Fax"  SortExpression ="Fax"  NullDisplayText ="N/A"   />
        
</ Columns >
    
</ asp:GridView >
        
    
</ div >
    
</ form >
</ body >
</ html >

cs代码:
using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;

using  System.Data.SqlClient;

namespace  BlogNet.ASPXDemo
{
    
public   partial   class  dataAccess1 : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
string  strConn  =   " Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True " ;
            SqlConnection conn 
=   new  SqlConnection(strConn);
            conn.Open();

            
string  sql  =   " select * from Customers " ;
            SqlDataAdapter da 
=   new  SqlDataAdapter(sql, conn);
            DataSet ds 
=   new  DataSet();
            da.Fill(ds);

            GridView1.DataSource 
=  ds;
            GridView1.DataBind();

            conn.Close();
        }

        
protected   void  GridView1_PageIndexChanging( object  sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex 
=  e.NewPageIndex;
            GridView1.DataBind();
        }
    }
}




目录
相关文章
|
4天前
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
9 0
|
4天前
|
存储 测试技术 计算机视觉
高维数据惩罚回归方法:主成分回归PCR、岭回归、lasso、弹性网络elastic net分析基因数据
高维数据惩罚回归方法:主成分回归PCR、岭回归、lasso、弹性网络elastic net分析基因数据
|
4天前
|
SQL 数据库
使用ADO.NET查询和操作数据
使用ADO.NET查询和操作数据
12 0
|
4天前
|
SQL 开发框架 .NET
ASP.NET WEB+EntityFramework数据持久化——考核练习库——1、用户管理系统(考点:查询列表、增加、删除)
ASP.NET WEB+EntityFramework数据持久化——考核练习库——1、用户管理系统(考点:查询列表、增加、删除)
72 0
|
4天前
|
C#
.NET开发中合理使用对象映射库,简化和提高工作效率
.NET开发中合理使用对象映射库,简化和提高工作效率
|
4天前
|
Oracle 关系型数据库 数据管理
.NET医院检验系统LIS源码,使用了oracle数据库,保证数据的隔离和安全性
LIS系统实现了实验室人力资源管理、标本管理、日常事务管理、网络管理、检验数据管理(采集、传输、处理、输出、发布)、报表管理过程的自动化,使实验室的操作人员和管理者从繁杂的手工劳作中解放出来,提高了检验人员的工作效率和效益,降低了劳动成本和差错发生率。
|
7月前
|
前端开发 JavaScript
.net core 前端传递参数有值 后端接收到的数据却是null
1、问题分析 在做接口测试时,偶然出现了前端输出有值,但是后端断点调试时却出现接收参数总是为null的情况 2、解决办法 前端打印log,看前端的每一个传值的数据类型,与后端请求参数类进行认真的一一比对 小技巧: ① 直接打印调用接口的传参值的数据类型,例如 console.log(type of this.form.name) --string console.log(type of this.form.age) --number 打印的数据类型与后端接口的参数类比对,查出不对应的类型 ② 关于非必填的值,默认传值可能出现空字符串(' ')、NaN值(Not a Number
114 0
|
7月前
|
SQL 开发框架 监控
|
8月前
|
JSON 数据格式
.NET Core - 配置绑定:使用强类型对象承载配置数据
.NET Core - 配置绑定:使用强类型对象承载配置数据
|
.NET
一起谈.NET技术,ASP.NET 4.0 一些隐性的扩展
  ASP.NET 4.0在很多方面都做了改进,在这篇ASP.NET 4.0白皮书就描述了很多ASP.NET 4.0的机制改变和改进。在我的博客中,也有几篇关于ASP.NET4.0的特性修改的文章。但是作为一个全新的框架和运行时,内部肯定还会有很多API和扩展点不会暴露的那么明显。
829 0