当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-29

    本文导语:  ExecuteReader执行查询实例ExecuteReader方法存在的目的只有一个:尽可能快地对数据库进行查询并得到结果。ExecuteReader 返回一个DataReader对象:如果在SqlCommand对象中调用,则返回SqlDataReader;如果在OleDbCommand对象中调用,返回的是Ole...

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执行查询
{
    ///
    /// Window1.xaml 的交互逻辑
    ///
    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@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);
                        }
                    }
                }
            }
        }
    }
}

    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3