zoukankan      html  css  js  c++  java
  • 用DataReader读取数据

    DataReader读取数据步骤

      1、连接数据源

      2、打开连接

      3、发出一个SQL查询命令

      4、使用DataReader读取并显示数据

      5、关闭DataReader和连接

    代码如下:

            private void DataReaderUse()
            {
                
    //1、建立连接
                SqlConnection thisConnection = new SqlConnection(sSqlConnection);
                
    //2、打开连接
                thisConnection.Open();

                
    //建立SQL查询
                SqlCommand thisCommand = thisConnection.CreateCommand();
                
    //3、输入查询语句
                thisCommand.CommandText = "select customerID,companyName from customers";

                
    //建立读取
                SqlDataReader thisReader = thisCommand.ExecuteReader();

                tbValue.Text 
    = "CustomerID" + "CompanyName";
                
    //4、查询并显示数据
                while (thisReader.Read())
                {
                    tbValue.Text 
    += "\r\t";
                    tbValue.Text 
    += thisReader["CustomerID"].ToString().PadRight(10' ');
                    tbValue.Text 
    += thisReader["CustomerID"].ToString();
                }
                
    //5、关闭连接
                thisReader.Close();
                thisConnection.Close();
            }
  • 相关阅读:
    下载安装Git,学习笔记
    php面试相关整理
    2.5 DQL 分组查询
    2.4 DQL 常见函数
    2.3 DQL 排序查询
    2.2 DQL 条件查询
    2.1 DQL 基础查询
    1.2 MySQL的介绍
    1.1 数据库的相关概念
    2019年JavaEE学习线路
  • 原文地址:https://www.cnblogs.com/scottckt/p/1260447.html
Copyright © 2011-2022 走看看