1try
2 {
3 SqlDataReader sdr=sqlcmd.ExecuteReader();
4 int cols=sdr.FieldCount;
5DataTable Mytab=new DataTable();
6 Mytab.BeginLoadData();
7 for(int i=0;i<cols;i++)
8 {
9 Mytab.Columns.Add(sdr.GetName(i),sdr.GetType());
10 }
11 string [] lvitem=new string[cols];
12 object [] values=new object[cols];
13 while(sdr.Read())
14 {
15 DataRow myRow=Mytab.NewRow();
16 sdr.GetValues(values);
17 for(int i=0;i<values.Length;i++)
18 myRow[i]=values[i].ToString();
19
20 Mytab.Rows.Add(myRow);
21 }
22 Mytab.EndLoadData();
23 this.dg.DataSource=Mytab;
24 sdr.Close();
25
26 }
27 catch(System.Data.SqlClient.SqlException ex)
28 {
29 MessageBox.Show(ex.Message);
30 }
31
32
2 {
3 SqlDataReader sdr=sqlcmd.ExecuteReader();
4 int cols=sdr.FieldCount;
5DataTable Mytab=new DataTable();
6 Mytab.BeginLoadData();
7 for(int i=0;i<cols;i++)
8 {
9 Mytab.Columns.Add(sdr.GetName(i),sdr.GetType());
10 }
11 string [] lvitem=new string[cols];
12 object [] values=new object[cols];
13 while(sdr.Read())
14 {
15 DataRow myRow=Mytab.NewRow();
16 sdr.GetValues(values);
17 for(int i=0;i<values.Length;i++)
18 myRow[i]=values[i].ToString();
19
20 Mytab.Rows.Add(myRow);
21 }
22 Mytab.EndLoadData();
23 this.dg.DataSource=Mytab;
24 sdr.Close();
25
26 }
27 catch(System.Data.SqlClient.SqlException ex)
28 {
29 MessageBox.Show(ex.Message);
30 }
31
32