![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
DataReader or DataSet?
Ok, so I have this code, the problem is every time it executes and recurses it throws an exception because you can only have one DataReader open at a time, I do not want to go through the process of saving all the data to my local memory, it can be a lot of data which is annoyng, but I need to know how to perform multiple queries at once.
Would switch to a DataSet using the disconnected framework assist with that? Or is there another way to make DataReader allow for multiple queries without creating an indefinte number of connections (a very stupid approach in my opinion). This is the code that I currently have: public productForm( mainForm parentForm )
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//set up the odbc connection reference
this.MdiParent = parentForm;
this.odbcConnection = parentForm.loginData.odbcConnection;
try
{
//grab a list of product categories from the database.
OdbcDataReader odbcResult;
OdbcCommand odbcCommand = new OdbcCommand( "", this.odbcConnection );
odbcCommand.CommandText = "SELECT categories.*, categories_description.* FROM categories INNER JOIN categories_description on categories.categories_id=categories_description.categories_id where categories.parent_id=0";
odbcResult = odbcCommand.ExecuteReader( );
while( odbcResult.Read( ))
{
ListBox newItem = new ListBox( );
newItem.Text = odbcResult.GetString( 8 );
this.categoryList.Items.Add( newItem );
this._RecurseListCategories( odbcResult.GetInt32( 0 ).ToString( ), odbcResult.GetString( 8 ),
this.odbcConnection );
}
}
catch ( OdbcException ex )
{
//database error, handle it now...
for( int i = 0; i < ex.Errors.Count; i++ )
MessageBox.Show( ex.Errors[i].Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
//update our current catergory list no matter what
this.categoryList.Update( );
}
}
/// <summary>
/// Recursively find category listings
/// </summary>
/// <param name="parentID">Current listing (will find children)</param>
private void _RecurseListCategories( string parentID, string currentName, OdbcConnection connection )
{
OdbcDataReader odbcResult;
OdbcCommand odbcCommand = new OdbcCommand( "", this.odbcConnection );
odbcCommand.CommandText = "SELECT categories.*, categories_description.* FROM categories INNER JOIN categories_description on categories.categories_id=categories_description.categories_id where categories.parent_id=" + parentID;
odbcResult = odbcCommand.ExecuteReader( );
while( odbcResult.Read( ))
{
ListBox newItem = new ListBox( );
newItem.Text = currentName + " : " + odbcResult.GetString( 8 );
this.categoryList.Items.Add( newItem );
this._RecurseListCategories( odbcResult.GetInt32( 0 ).ToString( ),
currentName + " : " + odbcResult.GetString( 8 ), this.odbcConnection );
}
}Any help is greatly appreciated... thanks.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
I don't know about this, but I think you can add another datareader like this:
OdbcCommand odbc_Second_Command = new OdbcCommand("");
odbc_Second_Command.CommandText = "SELECT"...;I think that would work. But I was doing something similar in SQL that worked that way. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|