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();
            }
  • 相关阅读:
    2017.10.30 天晴 昨天十公里没减肥
    我的一辩论点,随心而论
    2017.10.27 多云 天气晴
    2017.10.14 多云 天气转冷
    2017.10.9 天晴 准备减肥,有一起打卡的吗
    2017.10.7 国庆第8天
    2017.10.7 国庆第7天{鳏寡孤独}
    java多线程概念
    spring mvc分拣查询参数
    spring mvc 导出excel
  • 原文地址:https://www.cnblogs.com/scottckt/p/1260447.html
Copyright © 2011-2022 走看看