Sample sharing of executereader execution queries in c
- 2020-06-15 10:07:15
- OfStack
ExecuteReader executes the query instance
The ExecuteReader method exists for one reason only: to query the database as quickly as possible and get results. ExecuteReader returns 1 DataReader object: SqlDataReader if called in an SqlCommand object; If invoked in an OleDbCommand object, OleDbDataReader is returned. You can call DataReader's methods and properties to iterate over the result set. It is a quick enumeration of database query results mechanism, is read-only, only in. Each call to SqlDataReader.Read returns 1 row from the result set.
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 Execute the query
{
/// <summary>
/// Window1.xaml Interaction logic of
/// </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 The returned object type is SqlDataReader
{
//Read is bool Type, with an initial pointer to a 1 A piece of data before each call 1 time reader And the pointer goes down 1 Bar, as long as it doesn't move to the end 1 After the bar, it returns true .
while (reader.Read())
{
string name = reader.GetString(1);//GetString(1) You get the number in the table 1 The value of the column name Yes, because it's looking up * And the number of columns in the table 1 The sample.
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 +"'";// String concatenation finds the database.
cmd.CommandText = "select age from T_Student where name=@name or age>@age";// Compare the data with the database.
//@ parameter : Cannot be used to replace table names, field names, select Keyword, etc.
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 Is obtained by int type
//GetInt64 Is obtained by long Type ( bigint )
int age = reader.GetInt32(0);//GetInt32(0) The parameter is look cmd.CommandText There are several query results in.
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 ' zhang %'";
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);
}
}
}
}
}
}
}