• 用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();
            }
  • 相关阅读:
    html 中 url、scr、href、rel、rev
    MIME 和文件扩展名
    视频文件的容器格式和编码格式
    原型与原型链
    属性特征
    可选参数
    函数的定义(函数是值)
    闭包
    实现异步加载js文件及加载完成后回调
    前端工程打开速度优化的循序渐进总结
  • 原文地址:https://www.cnblogs.com/scottckt/p/1260447.html
Copyright © 2020-2023  润新知