Be careful, a bug in Microsoft daab 2.0!

Source: Internet
Author: User

Some time ago, we used the sqlhelper object in daab (Data Access Application Block) 2.0 to fill in the data, because the filldataset method is used in sqlhelper of MySQL 2.0, this is especially useful when you want to use a stored procedure to return multiple data tables at a time. In one of its overload methods, you can input a list of table names of the string [] type. However, when filling the dataset, it is found that as long as there are more than three tables in the dataset, the problem always occurs, at first, I thought that the field type or number of fields of my strong-type dataset were inconsistent with the fields in the tables retrieved during the stored procedure. However, I did not find any problems after careful check. I am wondering that it is okay to fill any two of the three tables using the sqlhelper. filldataset method? So I expanded all the retrieved result sets and found that there was an additional table named "Table2". That is to say, there were four tables read through the sqlhelper object, the third table is pushed to the fourth position, and the content of table 3 has been correctly filled, but the table name of table 3 has become "Table2 "!

I realized that it should be a problem with sqlhelper, so I found the last overload method of filldataset (the last physical location of the sqlhelper. CS file ):

Private   Static   Void Filldataset (sqlconnection connection, sqltransaction transaction, commandtype,
String Commandtext, dataset, String [] Tablenames,
Params Sqlparameter [] commandparameters)
{
If (Connection =   Null ) Throw   New Argumentnullexception ( " Connection " );
If (Dataset =   Null ) Throw   New Argumentnullexception ( " Dataset " );

// Create a command and prepare it for execution
Sqlcommand command =   New Sqlcommand ();
Bool Mustcloseconnection =   False ;
Preparecommand (command, connection, transaction, commandtype, commandtext, commandparameters, Out Mustcloseconnection );

// Create the dataadapter & Dataset
Using (Sqldataadapter dataadapter =   New Sqldataadapter (command ))
{

// Add the table mappings specified by the user
If (Tablenames ! =   Null   && Tablenames. Length >   0 )
{
String Tablename =   " Table " ;
For ( Int Index = 0 ; Index < Tablenames. length; index ++ )
{
If (tablenames [Index] = NULL | tablenames [Index]. length = 0) throw new argumentexception ("The tablenames parameter must contain a list of tables, a value was provided as null or empty string. "," tablenames ");
Dataadapter. tablemappings. Add (tablename, tablenames [Index]);
Tablename + = (index + 1). tostring ();
}
}

// Fill the dataset using default values for datatable names, etc
Dataadapter. Fill (Dataset );

// Detach the sqlparameters from the command object, so they can be used again
Command. Parameters. Clear ();
}

If (Mustcloseconnection)
Connection. Close ();
}

Note thatRedStatement:
Tablename + = (index + 1). tostring ();
You just need to take a look. This is a very obvious bug! When we have two tables, dataadaper is normal during table name ing, because the tablename of the two tables is table and Table1, respectively, however, if you add a table or more, tablename becomes table12, table123, table1234 ,... in this case, the consequences will be unimaginable. No wonder there will be one more table 2 in my result dataset. Originally, Table2 was not mapped to the third table of my strong-type dataset.

Now that we have discovered a bug, We have to manually correct it. In fact, it is very simple. We only need to change the statement:
Tablename = "table" + (index + 1). tostring ();
In this way, everything is OK.

I don't know if MS has fixed this bug in daab2.0, but at least this version I downloaded last month still exists.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.