// 2015/07/05
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient; namespace DataReaderSample
{
class Program
{
static void Main(string[] args)
{
// 集合,强类型的集合
System.Collections.Generic.List<StudentModel> list = new List<StudentModel>(); string connectionString = "server=.;database=BookSample;uid=LJK;pwd=123456";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "select ID,StuName ,phone from students";
SqlCommand cmd = new SqlCommand(sql,connection); // 通过数据库中的游标来辅助查询结果
SqlDataReader reader = cmd.ExecuteReader(); // DataReader 通过 Read 方法来读数据库中的记录
while (reader.Read())
{
Console.WriteLine("成功读取了一条记录");
// 读取的数据保存在 DataReader 对象内
int stuId = reader.GetInt32(0);
string stuName = reader.GetString(1);
string Phone = reader.GetString(2); //上面的程序还可以用下面的代码代替(第二种方法)
//这种方法得出的是object类型,所以要强制转换一下
//int stuId = (int)reader[0];(值类型)
//string stuName = reader[1] as string;(引用类型)
//string Phone = reader[2]; //另外一种方式,下标为字段名,即使select语句修改也无影响
//string stuName = reader["StuName"] as string;(引用类型) // 将读取的数据表示为对象实例
StudentModel model = new StudentModel();
model.StuID = stuId;
model.Stuname = stuName;
model.phone = Phone; list.Add(model); // 输出字段
// Console.WriteLine("StuId:{0} StuName = {1} Phone = {2}",stuId,stuName,Phone);
// Console.WriteLine("StuId:{0} StuName = {1} ", stuId, stuName);
}
// 游标也必须关闭
reader.Dispose();
}
// 当读取完成的时候,我们得到一个集合,其中包含若干个的对象实例
// 这些对象实例的数据来自数据库 foreach (StudentModel model in list)
{
Console.WriteLine("{0} {1} {2}",model.StuID,model.Stuname,model.phone);
}
Console.ReadKey();
}
}
}
/*
数据库中的数据是 null,与.NET下的null不一致
数据库中的null在.NET环境下,专门使用一个特殊的类型来表示
System.DBNull.Value
可用如下方法解决:
string region = null;
if(!reader.IsDBNull(3))
{
region = reader.GetString(3);
}
//假如为整形方法如下:
可空类型,只能用于值类型
int? reportsTo -= null;
//对于可控类型来说,现在也有两个状态
//reportsTo.HasValue
//reportsTo.Value if(!reader.IsDBNull(4))
{
reportsTo = reader.GetInt32(4);
}
*/ //另一个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DataReaderSample
{
public class StudentModel
{
private int stuId;
public int StuID
{
get { return stuId;}
set { stuId = value;}
}
public string Stuname { set; get; }
public string phone { set; get; } }
} //仅仅读取第一行第一列的值方法如下
// 2015/07/05
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient; namespace DataReaderSample
{
class Program
{
static void Main(string[] args)
{
string connectionString = "server=.;database=BookSample;uid=LJK;pwd=123456";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "select ID,StuName ,phone from students";
SqlCommand cmd = new SqlCommand(sql, connection); //仅仅读取第一行第一列的值
object obj = cmd.ExecuteScalar();
//如果一条数据都没有读到,那么返回null
if (obj != null)
{
int id = (int)obj;
Console.WriteLine("obj = {0}", id);
}
else
{
Console.WriteLine("没有读到数据!");
}
// 通过数据库中的游标来辅助查询结果
//SqlDataReader reader = cmd.ExecuteReader(); // DataReader 通过 Read 方法来读数据库中的记录
//if (reader.Read())
//{
// int stuid = reader.GetInt32(0);
// Console.WriteLine(stuid);
//}
//reader.Dispose();
} Console.ReadKey();
}
}
}

  

DataReader的用法程序简析的更多相关文章

  1. <摘自>飞:jxl简析2 [ http://www.emlog.net/fei ]

    [<摘自>飞:jxl简析:http://www.emlog.net/fei] (二)应用 在进行实践前 , 我们需要对 excel 有一个大致的了解 ,excel 文件由一个工作簿 (Wo ...

  2. 简析.NET Core 以及与 .NET Framework的关系

    简析.NET Core 以及与 .NET Framework的关系 一 .NET 的 Framework 们 二 .NET Core的到来 1. Runtime 2. Unified BCL 3. W ...

  3. 简析 .NET Core 构成体系

    简析 .NET Core 构成体系 Roslyn 编译器 RyuJIT 编译器 CoreCLR & CoreRT CoreFX(.NET Core Libraries) .NET Core 代 ...

  4. Android 启动过程简析

    首先我们先来看android构架图: android系统是构建在linux系统上面的. 所以android设备启动经历3个过程. Boot Loader,Linux Kernel & Andr ...

  5. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  6. SimpleDateFormat使用简析

    title: SimpleDateFormat使用简析 date: 2016-07-11 11:48:20 tags: Java SimpleDateFormat --- [转载自博客:http:// ...

  7. 简析TCP的三次握手与四次分手【转】

    转自 简析TCP的三次握手与四次分手 | 果冻想http://www.jellythink.com/archives/705 TCP是什么? 具体的关于TCP是什么,我不打算详细的说了:当你看到这篇文 ...

  8. 《共享库PATH与ld.so.conf简析》

    这是摘抄<共享库PATH与ld.so.conf简析>1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个 ...

  9. SpringMVC源码情操陶冶-DispatcherServlet类简析(一)

    阅读源码有利于陶冶情操,此文承接前文SpringMVC源码情操陶冶-DispatcherServlet父类简析 注意:springmvc初始化其他内容,其对应的配置文件已被加载至beanFactory ...

随机推荐

  1. Android点击Button按钮的四种事件监听方法总结

    首先我们在activity_main.xml里面先定义一个Button空间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <RelativeLayout xmlns:a ...

  2. AFNetworking content type not support

    ref:http://blog.csdn.net/nyh1006/article/details/25068255 1.错误信息:Error:Error Domain=AFNetworkingErro ...

  3. 微信小程序问题2:未配置

    app.json 中未配置当前页面 pages/swipertest/swipertest 请检查后重试.console.error(`PAGE DEFINE ERRORapp.json 中未配置,当 ...

  4. Ubuntu14.04 64bit编译u-boot-2016.07提示 Your dtc is too old, please upgrade to dtc 1.4 or newer

    Author:AP0904225版权声明:本文为博主原创文章,转载请标明出处. Ubuntu14.04 64bit环境下编译u-boot-2016.07提示如下错误: CHK include/conf ...

  5. 今天学习了下,如何破解wifi

    破解了隔壁的wif,得罪了,哥哥要蹭网一段时间. 主要思路:安装linux环境.这里我选择了虚拟机加cdlinux .运行里面的min抓得握手包,然后淘宝花10元跑下包,因为自己的电脑不行,跑费时间太 ...

  6. SQL Server 2012 - 数据库的基础操作

    数据库基本操作 --新建数据库卡 use master go create database SchoolDB on ( Name=SchoolDB, FileName='D;\DB\SchoolDB ...

  7. Django中的许可(Permissions)和用户组(Group)

    Reference: http://www.cnblogs.com/esperyong/archive/2012/12/20/2826690.html 接着上面的3篇讨论文章,我们阐述了Django中 ...

  8. 判断移动端设备: navigator.userAgent.toLowerCase()

    判断你的浏览设备: navigator.userAgent.toLowerCase(); (返回当前用户所使用的是什么浏览器,将获得的信息变成小写) function browserRedirect( ...

  9. 给 Memo 排序的函数

    本例效果图: 代码文件: unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  10. Thinkphp代码生成工具 ThinkphpHelper

    支持MySQL 和 sqlite数据库,快速构建项目原型,直接生成前后台CRUD代码片段,还可根据需要自行定制代码模板,减少重复劳动. 写这个东西的原因是因为我最近沮丧的发现很多时候我都在做重复的事情 ...