• DataTable和DataReader的遍历


    1、DataTable的遍历

                //创建数据表
                DataTable dt = GetDataTable("select * from Student");
                //存储数据
                StringBuilder sb = new StringBuilder();
                //循环遍历<br>    
                //******逐行遍历***************************************
                foreach (DataRow row in dt.Rows)//获取每一行
                {
                    for (int i = 0; i < dt.Columns.Count; i++)//获取每一行的逐个字段
                    {
                        sb.AppendFormat("{0}|", row[i]);//字段之间用 |分割
                    }
                    sb.Append("
    ");//每一行数据换行
                }
                Console.WriteLine(sb.ToString());
    
                //*******通过转换为数组遍历***********************************
                foreach (DataRow row in dt.Rows)
                {
                    // ArrayList list = new ArrayList(row.ItemArray);
    
                    for (int i = 0; i < row.ItemArray.Length; i++)//转换为数组
                    {
                        sb.AppendFormat("{0}|", row.ItemArray[i].ToString());//数组遍历
                    }
                    sb.Append("
    ");//每一行回车换行

    2、DataReader的遍历

                //获取数据集
                SqlDataReader dr = GetDataReader("select * from Student");
                //存储数据
                StringBuilder sb = new StringBuilder();
                
                if (dr.HasRows)//如果有数据 
                {
                    while (dr.Read()) 
                    {
                        for (int i = 0; i < dr.FieldCount; i++) //逐个字段的遍历
                        {
                            sb.AppendFormat("{0}|",dr[i]);//字段之间用 |连接
                        }
                        sb.Append("
    ");//每一行数据换行
                    }
                }
    //查询数据库返回数据
    
    class SQLhelper
        {
            private static string connString = ConfigurationManager.ConnectionStrings["connectStr"].ToString();
            //查询数据库返回SqlDataReader
            public static SqlDataReader GetReader(string sql)
            {
                SqlConnection conn = new SqlConnection(connString);
                SqlCommand cmd = new SqlCommand(sql, conn);
                try
                {
                    conn.Open();
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch (Exception ex)
                {
                    conn.Close();
                    throw ex;
                }
            }
            //查询数据库返回DataSet
            public static DataSet GetDataSet(string sql, string tablename)
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(sql, conn))
                    {
                        DataSet ds = new DataSet();
                        conn.Open();
                        sda.Fill(ds, tablename);
                        return ds;
                    }
                }
            }
        }    
    
    
    //App.config:
    
    <configuration>
      <connectionStrings>
        <add name="connectStr" connectionString="Data Source=.;Initial Catalog=MyDataBase;Integrated Security=True"/>
      </connectionStrings>
    </configuration>

    参考:http://www.xuebuyuan.com/445207.html

  • 相关阅读:
    GeoProcessor执行工具参数设置
    matlab为影像制作散点图
    无法连接SVN
    STM32例程之USB HID双向数据传输(源码下载)【转】
    USB仪器控制教程
    STemWin5.22移植笔记(flyheart)
    STemWin5.22移植笔记【转】
    [STemWin教程入门篇]第二期:emWin5.xx的详细移植步骤
    [STemWin教程入门篇] 第一期:emWin介绍
    TI推出一款强大模拟设计与仿真工具TINA-TI 9.
  • 原文地址:https://www.cnblogs.com/xifengyeluo/p/7883619.html
Copyright © 2020-2023  润新知