Translating into DataReader

Results 1 to 2 of 2

Thread: Translating into DataReader

  1. #1
    Join Date
    Dec 1969

    Default Translating into DataReader

    &nbsp;<BR>Loop through the rows of the DataTable<BR>For i = 0 To ds.Tables(0).Rows.Count - 1<BR> curCat = ds.Tables(0).Rows(i).Item("CategoryName")<BR> If curCat &#060;&#062; prevCat Then<BR> prevCat = curCat<BR> <BR> &#039;Create a new row<BR> Dim shRow As DataRow = ds.Tables(0).NewRow<BR> shRow("ProductName") = ds.Tables(0).Rows(i).Item("CategoryName")<BR> shRow("UnitPrice") = "SubHead"<BR> <BR> ds.Tables(0).Rows.InsertAt(shRow, i)<BR> End If<BR>Next i<BR><BR>What changes I have to do to translate the above code into a code that works with a DataReader

  2. #2
    Join Date
    Dec 1969

    Default RE: Translating into DataReader

    This i my method that creates DataTable from DataReader. Sorry its in C#:<BR> public DataTable getTable(string sql,string tblName) <BR> {<BR> SqlCommand myCommand = new SqlCommand(sql, m_con);<BR> m_con.Open();<BR> SqlDataReader myReader;<BR> myReader = myCommand.ExecuteReader();<BR> DataTable tbl = new DataTable(tblName);<BR> DataColumn myDataColumn;<BR> DataRow myDataRow;<BR> try<BR> {<BR> int numCols = myReader.FieldCount;<BR> string[] colNames = new string[numCols];<BR> int count = 0;<BR> while(myReader.Read()) <BR> {<BR> count++;<BR> if(count==1)<BR> {<BR> for(int i=0;i&#060;numCols;i++)<BR> {<BR> myDataColumn = new DataColumn();<BR> myDataColumn.DataType = Type.GetType("System.String");<BR> colNames[i] = myReader.GetName(i);<BR> myDataColumn.ColumnName = colNames[i];<BR> tbl.Columns.Add(myDataColumn);<BR> }<BR> }<BR> myDataRow = tbl.NewRow();<BR> for(int i=0;i&#060;numCols;i++)<BR> {<BR> myDataRow[colNames[i]] = myReader.GetValue(i);<BR> }<BR> tbl.Rows.Add(myDataRow);<BR> }<BR> }<BR> catch(Exception e)<BR> {<BR> throw (e);<BR> }<BR> finally<BR> {<BR> myReader.Close();<BR> m_con.Close();<BR> } <BR> return tbl;<BR> }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts