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("\r\n");//每一行数据换行
            }
            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("\r\n");//每一行回车换行

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("\r\n");//每一行数据换行
                }
            }

 

//查询数据库返回数据

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

posted @ 2017-11-23 10:34  西枫叶落  阅读(3720)  评论(0编辑  收藏  举报