使用DataSet来打印出数据库某个表的所有数据

数据库中的表如下:
image
private static void 打印出DataTable中的所有数据()
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=图库;Integrated Security=True");
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "select * from 会员数据";
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataSet 数据集 = new DataSet();
            adapter.Fill(数据集);
            DataTable table = 数据集.Tables[0];//table就是一个二维数组!
            DataRow row = table.Rows[0];
            //Console.WriteLine("第0个表第0行的用户名是:{0}",row["会员名"]);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    Console.Write(table.Rows[i][j]);
                    Console.Write(",");
                }
                //Console.WriteLine(table.Rows[i]["会员名"]);
                Console.WriteLine();
            }
        }
使用Rows[i][j]虽然看起来很数学化,但是省略了不少功夫去一个个去写表中的字段名称
posted @ 2011-04-13 23:41  爱玩的安哥  阅读(577)  评论(0编辑  收藏  举报