The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Please help with simple C# /database question

I'm very new to C# and seriously, the database part is making me feel it as C!@#**# ;)

My requirement is simple. I have an access DB (.mdb), I have an odbcDataAdapter and I generated a dataset out of it.

All I want to do is, in some loop, access each fetched row and do something with it. What is the simplest way I can do this?  A small code snippet will help. I don't need a datagrid and all that fancy. I need to process each row, that's all.

I'm tired of looking at web pages that complicate this, or maybe I'm plain stupid. Either way, Please help!!

Sunday, February 13, 2005
 
 
foreach(DataRow row in dataSet.Tables[0].Rows)
{

// do some stuff with row here, eg
int x = (int)row[0];

}

The key is knowing that the structure of the dataset is DataSet -> Tables -> Rows and each of them is IEnumerable so foreach will work.

Hope that helps...
.NET Guy
Sunday, February 13, 2005
 
 
You can also use a DataReader for simple firehose cursor queries:

SqlConnection connection = new SqlConnection("connection string here");
SqlCommand command = new SqlCommand("select * from [Table]", connection);
SqlDataReader reader = command.ExecuteReader();

while(reader.Read())
{
    string s = (string)reader["VarcharColumn"];
    long l = (long)reader["BigIntColumn"];
    int i = (int)reader["IntColumn"];
}

reader.Close();
reader = null;
connection.Close();
connection = null;
command.Dispose();
command = null;
smallbiz
Sunday, February 13, 2005
 
 
Whoops...you'll also need a connection.Open() in there to get the connection to the db opened.  :-)
smallbiz
Sunday, February 13, 2005
 
 
Just curious about C# (never touched it, yet)..

what is the difference between those .Close()s and the .Dispose()?

From their names, I can guess some subtle differences - yet those seem to be leaky abstractions.  Wouldn't "Close()" be a better name for the Dispose() too?
i like i
Monday, February 14, 2005
 
 
Nope, because you might want to close the connection but keep a reference to it and re-open it later. Dispose() releases the underlying ODBC connection and, once called, you cannot use the connection again.

Close, OTOH, simply sets sets the connection state to closed. If it also released the underlying ODBC connection, the close/open process would be much slower than it is.

I reckon IDispose is the way to go here...
.NET Guy
Monday, February 14, 2005
 
 
Why doesn't .Dispose() happen transparently where there is no more references to it?  (IDispose, right?  I learn my C# _only_ from snippets on these forums, so I am bound to be way off.)

Imagine the snippet:

command.Dipose();
...
command.Something();

what would happen?  At compile-time or run-time?

Sounds like your .Dipose() is a .ReallyClose().  Kind of leaky?
i like i
Monday, February 14, 2005
 
 
The purpose of Dispose is to free up resources deterministically *before* finalization. Otherwise they get freed up when the object is finalized and before it is deleted.

The .NET garbage collector is generation-based, not reference-based, so there's no notion of a disposal method than can be called automagically when the last reference to an object is removed. Unlike COM, there is no reference counting as such in .NET.

Notionally this is because a generation-based GC is more efficient because it cleans up large amounts of stuff at the same time, rather than little and often.
.NET Guy
Monday, February 14, 2005
 
 
...and you avoid the nasty problem with circular references.
Chris Nahr Send private email
Monday, February 14, 2005
 
 
Thank you people! I appreciate the help, and hopefully I can now inch forward. I really hate these database constructs :(

Monday, February 14, 2005
 
 
Don't forget you can use the using() construct to auto-magically call Dispose for you:

using(SqlConnection connection = new SqlConnection("connection string here"))
{
  using(SqlCommand command = new SqlCommand("select * from [Table]", connection))
  {
    SqlDataReader reader = command.ExecuteReader();

    while(reader.Read())
    {
      string s = (string)reader["VarcharColumn"];
      long l = (long)reader["BigIntColumn"];
      int i = (int)reader["IntColumn"];
    }
  }
}
Cory Foy Send private email
Monday, February 14, 2005
 
 
All this talk about the advantages of garbage collection, and we're back to managing resources by hand. What a load of crap.

Is there any equivalent of C#'s 'using' in vb.net?
comp.lang.c refugee
Monday, February 14, 2005
 
 
The explicit finalization of resources in .NET is quite different from Java, I think.

What if someone forgets to finalize?
i like i
Tuesday, February 15, 2005
 
 
"Is there any equivalent of C#'s 'using' in vb.net?"

Yep.

MyObj myObj = new MyObj();

try
{
  //stuff with myObj
}
finally
{
  myObj.Dispose();
}

which is what using() comes out to be in the IL anyway. So in C# writing this:

try
{
  using(MyObj myObj = new MyObj())
  {
    //stuff with myObj
  }
}
catch(Exception ex)
{
  LogException(ex);
}

ends up as nested try blocks in IL:

try
{
  MyObj myObj = new MyObj();
  try
  {
      //stuff with myObj
  }
  finally
  {
      myObj.Dispose();
  }
}
catch(Exception ex)
{
  LogException(ex);
}


it's just that using() is prettier and less to think about.
Cory Foy Send private email
Tuesday, February 15, 2005
 
 
experts-exchange.com
Phibian
Tuesday, February 15, 2005
 
 
For an equivalent to C#'s "using" in VB.NET, try the following:

Dim con As New SqlConnection("server=localhost;database=northwind;integrated
security=sspi")
Dim xact As SqlTransaction = Nothing
Dim writer As StringWriter = Nothing

Try
  con.Open()
  xact = con.BeginTransaction()
  writer = New StringWriter(CultureInfo.CurrentCulture)
  'Do some work
Finally
  Disposer.Dispose(con)
  Disposer.Dispose(xact)
  Disposer.Dispose(writer)
End Try

Class Disposer

Public Shared Sub Dispose(ByVal DisposeThis As Object)
  If Not DisposeThis Is Nothing Then
    If TypeOf DisposeThis Is IDisposable Then
      DirectCast(DisposeThis , IDisposable).Dispose()
    End If
  End If
End Sub

Public Shared Sub Dispose(ByVal DisposeThis As IDisposable)
  If Not DisposeThis Is Nothing Then
    DisposeThis.Dispose()
  End If
End Sub

End Class
Mark Pearce Send private email
Friday, February 18, 2005
 
 
As a general clarification, a .Dispose method normally has little to do with garbage collection (GC).

*** Garbage collection ***
GC is designed for managed memory cleanup. It reclaims the
memory associated with an object instance, but only at an un-determined time in the future. So the GC will cleanup
the memory associated with an object instance at some non-deterministic time in the future, thus preventing memeory leaks.

*** Dispose ***
A .Dispose method, on the other hand, should be for the timely cleanup of un-managed resources, such as file handles or database connections. You would normally call .Dispose to cleanup these un-managed resources in a
deterministic manner, regardless of when GC decided to cleanup the memory resource. So if your class has a  Dispose method and you omit to call it, you will be left with one or more un-managed resources (e.g. a database
connection) still open, regardless of whether GC has taken place.
Mark Pearce Send private email
Friday, February 18, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz