SqlDataReader的使用
1.建立数据库连接;
2.设置数据库指令;
3.数据拾取器接收输出的数据;
4.遍历打印数据;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace test2
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=HZ-PC;Initial Catalog=mydb;Persist Security Info=True;User ID=sa;Password=123";//设置连接属性
conn.Open();//连接数据库
SqlCommand cmd = conn.CreateCommand();//从所建立的数据库连接中新建数据库命令变量
cmd.CommandText = "select * from db_line"; //赋值
SqlDataReader dr = cmd.ExecuteReader();//建立数据拾取器,接收数据
Console.WriteLine("ID 姓名 编号 票");
while(dr.Read())//循环依次打印数据
Console.WriteLine("{0} {1} {2} {3} ",dr[],dr[],dr[],dr[]);
dr.Close();
conn.Close();
}
}
}
SqlDataReader的使用的更多相关文章
- SqlDataReader和SqlDataAdapter
SqlDataReader 高效,功能弱,只读访问SqlDataAdapter 强大,要求资源也大一点 SqlDataReader 只能在保持跟数据库连接的状态下才可以读取... SqlDataAda ...
- 使用SqlDataReader和SqlDataAdapter的注意
1.当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用完毕应该马上调用SqlDataReader.Close()关闭它. 2.一个 ...
- 利用反射将Datatable、SqlDataReader转换成List模型
1. DataTable转IList public class DataTableToList<T>whereT :new() { ///<summary> ///利用反射将D ...
- SqlDataReader、SqlDataAdapter與SqlCommand的 区别
1.SqlDataReader,在线应用,需要conn.open(),使用完之后要关闭. SqlConnection conn = new SqlConnection(connStr); //conn ...
- SqlDataReader执行带输出参数存储过程 错误分析
在上一篇随笔:SqlDataReader读取分页数据,pageCount你是肿么了? 遇到了很让人头疼的问题:SqlDataReader执行带输出参数的存储过程总是获取不到输出参数的正确值.这里将解决 ...
- SqlDataReader读取分页数据,pageCount你是肿么了?
自己在折腾代码的时候发现,SqlDataReader读取分页数据,存储过程中的输出参数总页数pageCount获取不准确. 我已经问过百度,技术群等..... 都说SqlDataReader用过后关闭 ...
- 关于ADO.NET@SQL Server&SqlDataReader
先说基础的,说基础的明白了再深的也是一样的.SQL是关系型数据库,所以就决定了对其操作的时候ADO的一些类要相互联系,Connection 类Command对象(ExecuteReader()方法.E ...
- C#使用SqlDataReader读取数据库数据时CommandBehavior.CloseConnection参数的作用
主要用在ExecuteReader(c)中,如果想要返回对象前不关闭数据库连接,须要用CommandBehavior.CloseConnection: CloseConnection解决了流读取数据模 ...
- 获取SqlDataReader的列名
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionSt ...
随机推荐
- (转)JAVA实现Windows拨号、IP切换
原理: 通过调用windows下的dos命令实现拨号 PS:连接名称获取不一定都是适用,但苦于知道的dos命令太少了,只能将就这么用着. 如有更好的方法,烦请不吝赐教. public class Co ...
- [Java Basics2] Iterable, Socket, Reflection, Proxy, Factory DP
Parent interface of Collection: Iterable Interface A class that implements the Iterable can be used ...
- 【Python】:简单爬虫作业
使用Python编写的图片爬虫作业: #coding=utf-8 import urllib import re def getPage(url): #urllib.urlopen(url[, dat ...
- fancybox 最基本的使用步骤
初步使用第一步 :引用js和样式 第二步 :设定要触发显示的元素(a标签,链接href指向div的id) <div><a href="#adddivtest" ...
- cocos2dx 3.0 之 lua 创建类 (二)
利用lua 中的table 特性 Base = {x = 0,y = 0} Base.name = "luohai"Base.age = 12Base.sex = "ma ...
- cocos2d/x 自带字体(label)
CCLabelTTF* label1 = CCLabelTTF::labelWithString("1掼蛋as", "AppleGothic", 15); la ...
- Socket编程基础——面向连接TCP
WinSock是Windows环境下的网络编程接口,它最初是基于Unix环境下的BSD Socket,是一个与网络协议无关的编程接口.WinSock包含两个主要版本,即WinSock1和WinSock ...
- asp.net 前台通过Eval()绑定动态显示样式
1.a标签链接 <%#Eval("ConfigCode").ToString().ToLower() == "publishtext" ? "& ...
- 有100个节点的AVL树最大深度是多少?
首先说AVL树的概念 1 左右子树的深度差<=1 2 左右子树都是AVL树. 其实这样算,可以倒推的. 空树 DEPTH = 0; AVL_DEPTH = 2^0+2^1+......+2^k ...
- jdbc 配置
jdbc 配置 Class.forName("com.mysql.jdbc.Driver") ;//加载数据库驱动 Connection conn=null; String ur ...