using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Com.Dys.Model
{
class User
{
private int id;
private string username;
private string password; public int Id
{
get { return this.id; }
set { this.id = value; }
} public string Username
{
get { return this.username; }
set { this.username = value; }
} public string Password
{
get { return this.password; }
set { this.password = value; }
} public User()
{
} public User(int id, string username, string password)
{
this.id = id;
this.username = username;
this.password = password;
}
}
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Com.Dys.Utils
{
class DBUtils
{
public static SqlDataReader GetSQLDataReader(string Sql)
{
try
{
string SqlConnection = "server=127.0.0.1;database=dingyingsi;uid=sa;pwd=dys123;";
//windows身份验证
//string sqlconn = "server=(local);database=keede1228;integrated security=SSPI;";
SqlConnection Connection = new SqlConnection(SqlConnection);
Connection.Open();
SqlCommand Command = new SqlCommand(Sql, Connection);
SqlDataReader Reader = Command.ExecuteReader();
return Reader;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
return null;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.Dys.Model;
using Com.Dys.Utils;
using System.Data.SqlClient; namespace Demo01
{
class Demo
{
static void Main(string[] args)
{
string Sql = "select id, username, password from [user]";
SqlDataReader Reader = DBUtils.GetSQLDataReader(Sql);
IEnumerable Users = Encapsulate(Reader);
foreach(User user in Users)
{
Console.WriteLine("id = {0}", user.Id);
Console.WriteLine("username = {0}", user.Username);
Console.WriteLine("password = {0}", user.Password);
}
Console.ReadLine();
} static IEnumerable Encapsulate(SqlDataReader Reader)
{
ArrayList al = new ArrayList();
User user = null;
if (Reader == null)
{
return null;
} while (Reader.Read())
{
user = new User();
user.Id = (int)Reader["id"];
user.Username = (string)Reader["username"];
user.Password = (string)Reader["password"]; al.Add(user);
}
return al;
}
}
}

C# 例子1的更多相关文章

  1. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  2. C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  3. REGEX例子

    作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...

  4. CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子

    CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...

  5. 简单例子了解View的事件分发

    什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...

  6. 简单的例子了解自定义ViewGroup(一)

    在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...

  7. kqueue例子

    网络服务器通常都使用epoll进行异步IO处理,而开发者通常使用mac,为了方便开发,我把自己的handy库移植到了mac平台上.移植过程中,网上居然没有搜到kqueue的使用例子,让我惊讶不已.为了 ...

  8. 今天有群友不是很清楚htm直接存数据库的危害,我简单举个例子

     通过这个案例就知道为什么不要把原生的html放数据库了  常见的几种转码  常用的几种显示方法 只有原生html和最下面一种弹框了,变成了持久xss 如果是Ajax的方式,请用@Ajax.JavaS ...

  9. ElasticSearch 5学习(5)——第一个例子(很实用)

    想要知道ElasticSearch是如何使用的,最快的方式就是通过一个简单的例子,第一个例子将会包括基本概念如索引.搜索.和聚合等,需求是关于公司管理员工的一些业务. 员工文档索引 业务首先需要存储员 ...

  10. 以实际的WebGIS例子探讨Nginx的简单配置

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 以实际项目中的一个例子来详细讲解Nginx中的一般配置,其中涉 ...

随机推荐

  1. 23种设计模式(1)-Facade设计模式

    前记 曾经我遇见的一个需求是这样的,接口A有个方法void methodA(),类B需要实现接口A的methodA()方法,并且在类B中需要把methodA()方法内部处理逻辑获得的结果利用C类实例的 ...

  2. Alpha冲刺 - (10/10)

    Part.1 开篇 队名:彳艮彳亍团队 组长博客:戳我进入 作业博客:班级博客本次作业的链接 Part.2 成员汇报 组员1(组长)柯奇豪 过去两天完成了哪些任务 本人负责的模块(共享编辑)的前端代码 ...

  3. POJ 1745 线性和差取余判断

    POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...

  4. shell 中的 && 和 ||

    shell 中的 && 和 || 简言之,shell 中 && --左边的命令执行成功才会执行右边的命令. || -- 左边的命令执行失败才会执行右边的命令.

  5. 使用nohup后台执行ftp传输命令

    因为有的时候会需要长时间传输文件,所以想用nohup 结合shell脚本一起使用,就不用一直在电脑面前了 . nohup 用法: nohup command & 然后就会出现 对应的 pid ...

  6. dialog里屏蔽ESC和回车

    重载PreTranslateMessage,在return之前加一句判断,只要是按下ESC和回车的消息,就直接置之不理即可,代码如下: if( pMsg->message == WM_KEYDO ...

  7. JS中图片飞飞效果

    当鼠标在界面上移动的时候,后面有一连串的图片跟随者一起飘动,效果如下: 实现的基本思想:准备五个img标签,为了方便控制都放在一个div里面,设置div的定位方式为 fixed,设置成这中定位方式主要 ...

  8. Could not load file or assembly 'Microsoft.AnalysisServices.SharePoint.Integration'

    In Central Administration, in System Settings, click Manage farm solutions. Click Powerpivotwebapp. ...

  9. Spring IOC 容器源码分析 - 填充属性到 bean 原始对象

    1. 简介 本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的.我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反 ...

  10. 防止sql注入的小函数 以及一些小验证

    function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialch ...