ExecuteReader执行查询实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
namespace ExecuteReader执行查询
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Student where Age<100";
using (SqlDataReader reader = cmd.ExecuteReader())//ExecuteReader返回的对象类型是SqlDataReader
{
//Read是bool类型,初始指针指向第一条数据之前,每调用一次reader,指针就下移一条,只要没有移到最后一条之后,就返回true。
while (reader.Read())
{
string name = reader.GetString(1);//GetString(1)得到表中第一列的值,用name接收,因为查的是*,所以就和表中的列数一样。
int age = reader.GetInt32(2);
MessageBox.Show(name+","+age);
}
}
}
}
}
private void btnQuery_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//cmd.CommandText = "select age from T_Student where name='"+ txtName.Text +"'";//字符串拼接查找数据库。
cmd.CommandText = "select age from T_Student where name=@name or age>@age";//与数据库进行数据对比。
//@参数:不能用来替换表名,字段名,select之类的关键字等。
cmd.Parameters.Add(new SqlParameter("@name",txtName.Text));
cmd.Parameters.Add(new SqlParameter("@age",Convert.ToInt32(txtAge.Text)));
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//GetInt32获得的是int类型
//GetInt64获得的是long类型(bigint)
int age = reader.GetInt32(0);//GetInt32(0)中的参数是看cmd.CommandText中的查询结果有几个。
MessageBox.Show(age.ToString());
}
}
}
}
}
private void btnHobby_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_Student where name like '张%'";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string hobby = reader.GetString(3);
MessageBox.Show(hobby);
}
}
}
}
}
private void btnQuery1_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select hobbit from T_Student where age>@age or hobbit =@hobbit";
cmd.Parameters.Add(new SqlParameter("@age", txtAge1.Text));
cmd.Parameters.Add(new SqlParameter("@hobbit",txtHobby.Text));
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string str = reader.GetString(0);
MessageBox.Show(str);
}
}
}
}
}
}
}
ExecuteReader执行查询实例的更多相关文章
- 三层登录实例VB.NET版详解---理论加实战篇
层,百度百科这样解释,首先-重叠起来的东西:重叠起来的东西中的一部分:层次|表层|大气层.其次-重叠:重复:层峦叠嶂|层出不穷.最后-量词,用于可以分出层次的事物,女孩儿强烈的第六感,三层中的层一定是 ...
- ADO.NET对象的详解
1. Connection 类 和数据库交互,必须连接它.连接帮助指明数据库服务器.数据库名字.用户名.密码,和连接数据库所需要的其它参数.Connection对象会被Command对象使用,这样就能 ...
- Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]
[虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...
- vb.net三层实现登录例子
看三层已经很长时间了,中间有经过了期末考试.回家等等琐事,寒假开学的我已经回想不起什么事三层了,经过了三四天的重新复习,再加上查看各期师哥师姐的博客,终于,自己完成了C#视频中的登录小例子,下面就和大 ...
- ORM框架-VB/C#.Net实体代码生成工具(EntitysCodeGenerate)【ECG】4.5
摘要:VB/C#.Net实体代码生成工具(EntitysCodeGenerate)[ECG]是一款专门为.Net数据库程序开发量身定做的(ORM框架)代码生成工具,所生成的程序代码基于OO.ADO.N ...
- ado.net(class0503)
ado.net组成 数据提供程序 connection //连接对象 command executeNonQuery //执行增删改 executeScalar //执行查询返回首行首列 execut ...
- ASP.NET - ADO.NET框架
ADO.NET(ActiveX Data Objects) ADO.NET是一个COM组件库,用于在使用Microsoft技术的代码中访问数据库. SQL数据操作类 SqlConnection类(Sy ...
- 使用C#开发数据库应用程序
第一章 用Hello ACCP.NET快速热身(一) 1-1.进入C#世界 a.第一个C#程序 (1)新建项目[项目:project] (2)生成解决方案[生成:build,解决方案:solution ...
- ADO.NET基础知识
ADO.NET定义 ADO是ActiveX Data Objects的缩写,ADO.NET是数据库应用程序和数据源之间沟通的桥梁,主要提供一个面向对象的数据访问架构,用来开发数据库应用程序,就是一组类 ...
随机推荐
- code first基础
随着.NET 4.0时代的到来,开发者越来越关注如何能加快开发效率,从而构建健壮的程序.而微软在.NET 4.0推出的Entity Framework,无疑是值得开发者去学习的,它实际上是微软的ADO ...
- C++标准库的数值极限numeric_limits
包含头文件:#include<limits> 它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个 ...
- 批处理WMIC查看补丁情况
最近补丁比较多,需要看系统打了些啥,哪些没打的BAT: wmic qfe GET hotfixid > a.txt&(for %i in (KB3076321 KB3072604 KB3 ...
- BZOJ 1033 杀蚂蚁
Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...
- 【HDOJ】3088 WORM
状态压缩+BFS. /* 3088 */ #include <iostream> #include <cstdio> #include <cstring> #inc ...
- 【HDOJ】2222 Keywords Search
AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...
- 【转】Android驱动开发之earlysuspend睡眠模式编程总结
原文网址:http://blog.csdn.net/bigapple88/article/details/8669537 (1)添加头文件: #include <linux/earlysuspe ...
- 【转】Android:Layout_weight的深刻理解
原文网址:http://mobile.51cto.com/abased-375428.htm 本文详细介绍了Android布局中Layout_weight的属性,它是用来分配属于空间的一个属性,你可以 ...
- 从字符串总分离文件路径、命名、扩展名,Substring(),LastIndexOf()的使用;替换某一类字符串,Replace()的用法
一:从字符串总分离文件路径.命名.扩展名,上图 二:代码 using System; using System.Collections.Generic; using System.ComponentM ...
- bzoj 3572 [Hnoi2014]世界树(虚树+DP)
3572: [Hnoi2014]世界树 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 645 Solved: 362[Submit][Status] ...