c#中executereader執(zhí)行查詢示例分享
ExecuteReader執(zhí)行查詢實例
ExecuteReader方法存在的目的只有一個:盡可能快地對數(shù)據(jù)庫進(jìn)行查詢并得到結(jié)果。ExecuteReader 返回一個DataReader對象:如果在SqlCommand對象中調(diào)用,則返回SqlDataReader;如果在OleDbCommand對象中調(diào)用,返回的是OleDbDataReader。可以調(diào)用DataReader的方法和屬性迭代處理結(jié)果集。它是一個快速枚舉數(shù)據(jù)庫查詢結(jié)果的機(jī)制,是只讀、只進(jìn)的。對SqlDataReader.Read的每次調(diào)用都會從結(jié)果集中返回一行。
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執(zhí)行查詢
{
/// <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類型,初始指針指向第一條數(shù)據(jù)之前,每調(diào)用一次reader,指針就下移一條,只要沒有移到最后一條之后,就返回true。
while (reader.Read())
{
string name = reader.GetString(1);//GetString(1)得到表中第一列的值,用name接收,因為查的是*,所以就和表中的列數(shù)一樣。
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 +"'";//字符串拼接查找數(shù)據(jù)庫。
cmd.CommandText = "select age from T_Student where name=@name or age>@age";//與數(shù)據(jù)庫進(jìn)行數(shù)據(jù)對比。
//@參數(shù):不能用來替換表名,字段名,select之類的關(guān)鍵字等。
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)中的參數(shù)是看cmd.CommandText中的查詢結(jié)果有幾個。
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);
}
}
}
}
}
}
}
- C#使用Process類調(diào)用外部exe程序
- C# IDE VS2005中的Hosting Process (vshost.exe)作用介紹
- C#中ExecuteNonQuery()返回值注意點分析
- C#事務(wù)處理(Execute Transaction)實例解析
- 解析c#操作excel后關(guān)閉excel.exe的方法
- c#啟動EXE文件的方法實例
- C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)
- C#實現(xiàn)合并多個word文檔的方法
- C#文件合并的方法
- C#程序(含多個Dll)合并成一個Exe的簡單方法
相關(guān)文章
C#實現(xiàn)類似新浪微博長URL轉(zhuǎn)短地址的方法
這篇文章主要介紹了C#實現(xiàn)類似新浪微博長URL轉(zhuǎn)短地址的方法,涉及C#操作正則表達(dá)式的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實例代碼
這篇文章主要介紹了C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實例代碼,需要的朋友可以參考下2018-02-02Visual Studio中根據(jù)系統(tǒng)區(qū)分引用64位、32位DLL動態(tài)庫文件的配置方法
這篇文章主要介紹了Visual Studio中根據(jù)系統(tǒng)區(qū)分引用64位、32位DLL動態(tài)庫文件的配置方法,本文在VS2008中測試通過,其它VS版本可以參考下2014-09-09C#編程實現(xiàn)統(tǒng)計文件夾內(nèi)文件和隱藏文件的方法示例
這篇文章主要介紹了C#編程實現(xiàn)統(tǒng)計文件夾內(nèi)文件和隱藏文件的方法,結(jié)合具體實例形式分析了C#針對文件與目錄的遍歷及屬性操作相關(guān)技巧,需要的朋友可以參考下2017-07-07