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?
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.