AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Chris
Expertise
I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.

Experience
I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.

Education/Credentials
I have some college education, but does it really matter in this field of work?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > About Dataset and Datatable size in vb.net

Topic: VB.NET



Expert: Chris
Date: 9/5/2007
Subject: About Dataset and Datatable size in vb.net

Question
Dear Sir,
 I am software developer from India. I want to ask you one question regarding datatable size and dataset size.
 Sir, I fill one dataset with multiple tables. I want to find out that how much size my each table occupies.
 I tried lot to find answer on above issue. but I didn't get the satisfactory answer.
 Kindly reply me.

Answer
Unfortunately, in the .NET Framework, there really is no way to easily calculate this.  You'd have a much easier time checking the size of the data table at the database level.  To do this on SQL Server, you can use the sp_spaceused stored procedure:

exec sp_spaceused [tablename]

If you actually want to calculate the space used by the data in a DataTable object, you'll have to do it manually by stepping through all of its fields, checking which data type it is, and making the calculation as appropriate.

   Dim ds As New DataSet

  ' put code here to fill the dataset with datatable(s)...

   Dim lngDataSize As Long = 0
   ' loop through the tables in the dataset
   For Each dt As DataTable In ds.Tables
     ' loop through the rows of each table
     For Each dr As DataRow In dt.Rows
       ' loop through each row's columns
       For i As Integer = 0 To dt.Columns.Count - 1
         ' get this column value
         Dim obj As Object = dr.Item(i)

         ' if the column is an array, we need to get
         ' its length and multiply it by the size of
         ' its element type, which can be done by using
         ' the first element in the array
         If (TypeOf obj Is Array) Then
           Dim arr As Array = DirectCast(obj, Array)
           lngDataSize += (arr.Length * Len(arr(0)))
         Else
           ' otherwise we can just check the length
           ' of this value, since sql server should
           ' only be returning arrays or intrinsic
           ' datatypes len() will return the size
           lngDataSize += Len(obj)
         End If
       Next
     Next
   Next

Note that this isn't a perfectly accurate representation of how much these tables consume.  There's overhead for the DataTable and DataSet objects themselves, and if they're stored in SQL Server there'll be some overhead in the datafile format they're stored in.  There's also the issue of varying text encodings and whatnot, your characters may be Unicode characters, which means they each take up 2 bytes instead of one when persisted to disk, or they may be ASCII characters which only take up 1 byte each.  But for most cases, this method should get you a nearly accurate measurement.

Hope this helps.

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.