Print Contents Of A DataTable

后端 未结 7 1794
再見小時候
再見小時候 2020-12-05 17:35

Currently I have code which looks up a database table through a SQL connection and inserts the top five rows into a Datatable (Table).

using(SqlCommand _cmd          


        
相关标签:
7条回答
  • 2020-12-05 18:09

    This is done by data table that holds single table

            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from info", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
    
            DataTable dt = new DataTable();
            ad.Fill(dt);
            Console.WriteLine(dt.Columns[0].ColumnName.ToString());
            Console.WriteLine(dt.Rows[1].ItemArray[0].ToString());
    

    This is done by data set that holds set of table

            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from info", con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
    
            DataSet dt = new DataSet();
            ad.Fill(dt);
            Console.WriteLine(dt.Tables[0].Columns[0].ColumnName.ToString());
            Console.WriteLine(dt.Tables[0].Rows[0].ItemArray[0].ToString());
    

    both will give same result. only data set contains number of index of table.

    0 讨论(0)
提交回复
热议问题