DataReader必须持续连接,所以在调用方法SqlDataReader作为返回类型时候,必须在方法外关闭流,很不方便。

DataAdapter用于对数据源检索数据并填充到DataSet中的表。DataAdapter还可以将DataSet所做的更改进行解析回数据源。

(通俗点,DataSet就是一个缓冲区,可以修改好数据,让DataAdapter返回回数据源)

DataAdapter使用例程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace DataAdapter
{
public partial class Form1 : Form
{
string constr = "server=QT-201303030913;database=ThreeDb;uid=sa;pwd=daxiang";
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//Bind();
} private void Bind()
{ string sql = "select * from product";
SqlDataAdapter sda = new SqlDataAdapter(sql, constr);
DataSet ds = new DataSet();
sda.Fill(ds); dataGridView1.DataSource = ds.Tables[];
} private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("select * from product", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds,"product"); dataGridView1.DataSource = ds.Tables["product"];
}
} private void button2_Click(object sender, EventArgs e)
{
SqlDataAdapter sda = new SqlDataAdapter("select * from product", constr);
DataSet ds = new DataSet();
sda.Fill(ds,"p"); dataGridView1.DataSource = ds.Tables["p"];
} private void button3_Click(object sender, EventArgs e)
{
SqlDataAdapter sda = new SqlDataAdapter("select * from product", constr);
DataSet ds = new DataSet();
sda.Fill(ds, "p");
dataGridView1.DataSource = ds.Tables["p"]; sda.SelectCommand.CommandText = "select * from classify";
sda.Fill(ds,"c");
dataGridView2.DataSource = ds.Tables["c"];
} private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string concell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
string msg = string.Format("您单击的是第{0}行的第{1}列\n当前单元格的内容为\"{2}\"",e.RowIndex.ToString(),e.ColumnIndex.ToString(),concell);
MessageBox.Show(msg);
} }
}

form1设计

在一个DataSet中多张表名存在大小写,则搜索表名倍认为存在大小写,否则补区分大小写。

C#DataSet/DataAdapter的更多相关文章

  1. ADO.NET中的DataSet和DataAdapter

    DataSet和DataTable DataSet是一个数据集合,存储在内存中,算是一个临时的数据库. 个人觉得,不是太常用了,DataTable或者直接用List<model>存数据比这 ...

  2. 继承IDbConnection连接不同数据库

    继承IDbConnection连接不同数据库 本方案可实现仅修改app.config即可连接不同数据库,但是设计数据库时需要注意各种数据库的数据类型是不一样的. 各种不同数据库的Connection. ...

  3. 设计模式学习之适配器模式(Adapter,结构型模式)(14)

    参考链接:http://www.cnblogs.com/zhili/p/AdapterPattern.html一.定义:将一个类的接口转换成客户希望的另一个接口.Adapter模式使得原本由于接口不兼 ...

  4. DataTable在内存中的使用

    DataTable表示一个与内存有关的数据表,可以使用工具栏里面的控件拖放来创建和使用,也可以在编写程序过程中根据需要独立创建和使用,最常见的情况是作为DataSet的成员使用,在这种情况下就需要用在 ...

  5. csharp:Nhibernate Procedure with CreateSQLQuery and GetNamedQuery

    <?xml version="1.0" encoding="utf-8"?> <hibernate-mapping assembly=&quo ...

  6. ADO.NET数据库

    ASP.NET提供了ADO.NET技术,它是ASP.NET应用程序与数据库进行交互的一种技术. ADO.NET技术把对数据库的操作分为几个步骤,并为每个步骤提供对象来封装操作过程,从而使对数据库的操作 ...

  7. .NET设计模式(16):模版方法(Template Method)(转)

    摘要:Template Method模式是比较简单的设计模式之一,但它却是代码复用的一项基本的技术,在类库中尤其重要. 主要内容 1.概述 2.Template Method解说 3..NET中的Te ...

  8. c#获得目标服务器中所有数据库名、表名、列名的实现代码

    /// <summary> /// 获得目标服务器所有数据库名 /// </summary> /// <param name="serverName" ...

  9. 通用SQL存储过程分页以及asp.net后台调用

    创建表格并添加300万数据 use Stored CREATE TABLE UserInfo( --创建表 id ,) PRIMARY KEY not null,--添加主键和标识列 UserName ...

随机推荐

  1. android端StarIO热敏打印机打印小票

    最近在做这个热敏打印机打印小票,开始的时候在网上找资料,发现国内基本没有这方面的资料,国外也很少,在此做个打印小票的记录. 这里只记录一些关键点. 使用StarIOPort.searchPrinter ...

  2. 一致性哈希算法PHP测试片段

    <?php header('Content-type: text/html; charset=utf8');# 抽象接口interface hash{ public function _hash ...

  3. 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

    [COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...

  4. spring boot 使用拦截器 实现 用户登录拦截

    登录拦截和和权限拦截实现类似   首先自定义一个[DefineAdapter]类,这个类我是用来放自定义的配置(比如 自定义请求参数,自定义拦截器等),集成WebMvcConfigurerAdapte ...

  5. executable null\bin\winutils.exe in the Hadoop binaries.

    在windows 使用eclipse远程调用hadoop集群时抛出下面异常 executable null\bin\winutils.exe in the Hadoop binaries. 这个问题 ...

  6. sparkStreaming的mapWithState函数【案例二】

    sparkStreaming是以连续bathinterval为单位,进行bath计算,在流式计算中,如果我们想维护一段数据的状态,就需要持久化上一段的数据,sparkStreaming提供的Mapwi ...

  7. MySQL_使用时遇到的问题汇总

    一.data too long for column 'name' at row 1 1.现象:把数据库的字符集编码设置为utf-8,通过DOS界面向表的某一列插入汉字时会遇到类似 data too ...

  8. linux中查找文件并合并文件

    find ./src -name '*.txt' -exec cat '{}' \; > test.txt

  9. 【Mac系统 + Mysql】之安装Mysql数据库

    安装Mysql步骤: 一.下载 参考文章<mac 安装MySQL> 到Mysql官网下载.dmg格式的文件 先放弃了,看下面的简易安装. 二.使用homebrew安装MySQL(推荐) 如 ...

  10. Maven学习----dependencies与dependencyManagement的区别(转)

    转自:http://blog.csdn.net/liutengteng130/article/details/46991829 1.DepencyManagement应用场景 当我们的项目模块很多的时 ...