3_04_MSSQL课程_Ado.Net_.ExcuteReader()(SQLDataReader)
ExcuteNonQuery(); 返回影响的行数
ExcuteSacalar();返回第一行第一列
ExcuteReader(); Reader,指针,指向表的表头。只是指向,数据仍在数据库中。
SqlDataReader reader=cmd.ExcuteReader();执行完之后,将指针指向数据库的表头(reader对象指向了表的表头行)默认指向表头(标题行)
reader.Read(); 指针往下走一步 ,返回的是bool类型
var str=reader["userId"]读取当前行(数据行一)列的值
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Configuration; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Drawing; 8 using System.IO; 9 using System.Linq; 10 using System.Text; 11 using System.Threading.Tasks; 12 using System.Windows.Forms; 13 14 namespace _02ProvinceCitySelect 15 { 16 public partial class MainFrm : Form 17 { 18 public MainFrm() 19 { 20 InitializeComponent(); 21 } 22 23 private void MainFrm_Load(object sender, EventArgs e) 24 { 25 //加载数据库中的所有的省的数据 26 string connStr = ConfigurationManager.ConnectionStrings["LJK_SQLServerDBConnectionString"].ConnectionString; 27 28 //创建链接对象 29 using (SqlConnection conn =new SqlConnection(connStr)) 30 { 31 using (SqlCommand cmd =conn.CreateCommand()) 32 { 33 conn.Open();//***********8 34 cmd.CommandText =@"select AreaId, AreaName, AreaPid from [dbo].[AreaFull] where AreaPId=0"; 35 using (SqlDataReader reader = cmd.ExecuteReader()) 36 { 37 while ( reader.Read()) 38 { 39 //int AreadId = int.Parse(reader["AreaId"].ToString()); 40 //把表格的数据转换成 对象数据 41 AreaInfo areaInfo = new AreaInfo(); 42 areaInfo.AreaId = int.Parse(reader["AreaId"].ToString()); 43 areaInfo.AreaName = reader["AreaName"].ToString(); 44 areaInfo.AreaPId = int.Parse(reader["AreaPId"].ToString()); 45 //把省的信息放到 ComboBox中。ComboBox显示信息是 Item对象的ToString() 46 this.cbxProvince.Items.Add(areaInfo); 47 } 48 }//end useing reader 49 }//end useing cmd 50 }//end using conn 51 52 this.cbxProvince.SelectedIndex = 0; 53 } 54 55 private void cbxProvince_SelectedIndexChanged(object sender, EventArgs e) 56 { 57 AreaInfo provinceAreaInfo= this.cbxProvince.SelectedItem as AreaInfo; 58 //判断是否 拿到的城市为空 59 if (provinceAreaInfo == null) 60 { 61 return; 62 } 63 //根据省的Id获取所有的城市信息 64 65 //加载数据库中的所有的省的数据 66 string connStr = ConfigurationManager.ConnectionStrings["LJK_SQLServerDBConnectionString"].ConnectionString; 67 68 //创建链接对象 69 using (SqlConnection conn = new SqlConnection(connStr)) 70 { 71 using (SqlCommand cmd = conn.CreateCommand()) 72 { 73 conn.Open();//***********8 74 cmd.CommandText = @"select AreaId, AreaName, AreaPid from [dbo].[AreaFull] where AreaPId="+provinceAreaInfo.AreaId; 75 using (SqlDataReader reader = cmd.ExecuteReader()) 76 { 77 this.cbxCity.Items.Clear(); 78 while (reader.Read()) 79 { 80 //int AreadId = int.Parse(reader["AreaId"].ToString()); 81 //把表格的数据转换成 对象数据 82 AreaInfo areaInfo = new AreaInfo(); 83 areaInfo.AreaId = int.Parse(reader["AreaId"].ToString()); 84 areaInfo.AreaName = reader["AreaName"].ToString(); 85 areaInfo.AreaPId = int.Parse(reader["AreaPId"].ToString()); 86 //把省的信息放到 ComboBox中。ComboBox显示信息是 Item对象的ToString() 87 this.cbxCity.Items.Add(areaInfo); 88 } 89 }//end useing reader 90 }//end useing cmd 91 }//en 92 this.cbxCity.SelectedIndex = 0; 93 94 } 95 96 private void btbExport_Click(object sender, EventArgs e) 97 { 98 #region 选择保存文件 99 string fileName = string.Empty;// 保存的文件名 100 //让用户选择 要保存的文件路径 101 using (SaveFileDialog sfd = new SaveFileDialog()) 102 { 103 if (sfd.ShowDialog() != DialogResult.OK) 104 { 105 return; //嵌套的方式,尽量少的嵌套 106 } 107 fileName = sfd.FileName; 108 } 109 #endregion 110 111 // 查询数据数据,写入数据 112 string connStr = ConfigurationManager.ConnectionStrings["LJK_SQLServerDBConnectionString"].ConnectionString; 113 using (SqlConnection conn =new SqlConnection(connStr)) 114 { 115 using (SqlCommand cmd =conn.CreateCommand()) 116 { 117 conn.Open(); 118 cmd.CommandText = "select AreaId, AreaName, AreaPid from [dbo].[AreaFull]"; 119 using (SqlDataReader reader =cmd.ExecuteReader()) 120 { 121 string tempLine = string.Empty; 122 using (StreamWriter writer =new StreamWriter(fileName)) //文件流操作的学习 123 { 124 while (reader.Read()) 125 { 126 tempLine = reader["AreaId"] + "," + 127 reader["AreaName"] + "," + 128 reader["AreaPId"]; 129 writer.WriteLine(tempLine);//写入文本文件 130 } 131 } 132 133 } 134 } 135 } 136 } 137 } 138 }
reader.Read(); 指针继续往下走一行
var str=reader["userAge"]读取当前行(数据行二)列的值
在线的数据库读取,后面还有离线的数据库读取(一次性加载数据库)