c#中executereader执行查询示例分享

 更新时间:2014年04月02日 10:33:40   作者:  
这篇文章主要介绍了c#中executereader执行查询示例,需要的朋友可以参考下

ExecuteReader执行查询实例

ExecuteReader方法存在的目的只有一个:尽可能快地对数据库进行查询并得到结果。ExecuteReader 返回一个DataReader对象:如果在SqlCommand对象中调用,则返回SqlDataReader;如果在OleDbCommand对象中调用,返回的是OleDbDataReader。可以调用DataReader的方法和属性迭代处理结果集。它是一个快速枚举数据库查询结果的机制,是只读、只进的。对SqlDataReader.Read的每次调用都会从结果集中返回一行。

复制代码 代码如下:
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);
                        }
                    }
                }
            }
        }
    }
}

相关文章

最新评论