程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#中executereader履行查詢示例分享

c#中executereader履行查詢示例分享

編輯:C#入門知識

c#中executereader履行查詢示例分享。本站提示廣大學習愛好者:(c#中executereader履行查詢示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是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);
                        }
                    }
                }
            }
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved