Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DataTable/DataSet Question
Message
De
28/04/2007 10:03:54
 
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Divers
Thread ID:
01216542
Message ID:
01220774
Vues:
24
>Ok, but how do I 'attach' the data table to the DataAdapter? I'm not sure
>of the syntax.


There is no "attaching", I have no clue what you mean by that.

You have two options.

1) You can make your original oDataAdapter that you created with your oDataFactory scoped to your whole form (or maybe you're using a separate DataAccess class?), then you can re-use it in your UpdateData() method. If you do this, you might as well also create a DBCommandBuilder here as well ... this will automatically generate your insert,etc. commands for you (assuming that your data has a PK).
// Get a data adapter from the factory
DbDataAdapter oDataAdapter = oDataFactory.CreateDataAdapter();
oDataAdapter.SelectCommand = oCommand;

// Get a command builder from the factory
DBCommandBuilder oCommandBuilder = oDataFactory.CreateCommandBuilder();
oCommandBuilder.DataAdapter = oDataAdapter;
-or-

2) Create another DataAdapter and CommandBuilder in your UpdateData() method and use them there. I would do it this way, since my UI is totally disconnected from the DataAcess, which is in a separate class).
public bool UpdateData(DataTable oDataTable)
{
    // Declare variables
    bool bRetVal = true;

    // Create a dataset of changes
    DataTable oChanges = oDataTable.GetChanges(DataRowState.Modified);

    // If any rows exist in the changes cursor...
    if (oChanges.Rows.Count > 0)
    {

        // WHAT GOES HERE???
	// newly create an instance of DBDataAdapter and DBCommandBuilder, or use your previously instantiated ones
	oDataAdapter.Update(oDataTable);
    }

    return bRetVal;
}
I don't typically use the CommandBuilder, but as a tool to get you started it's good for newbies.

~~Bonnie


>
>
>
>>In your UpdateData() method, you could instantiate another DataAdapter, set up the properties for your UpdateCommand (you could use the SqlCommandBuilder to help you), then use that to do your dataadapter.Update().
>>
>>~~Bonnie
>>
>>
>>
>>>The question is, what do I do with this DataTable?
>>>
>>>My query code returns a DataTable, which I can then pass into the UpdateData
>>>procedure.
>>>
>>>Can I somehow 'attach' the data table to the DataAdapter?
>>>
>>>
>>>
>>>
>>>
>>>>The dataadapter has properties for Select, Insert, Update, delete;
>>>>
>>>>just create you update command:
>>>>
>>>>
>>>>dataadapter.UpdateCommand = new sqlCommand("UPDATE table.....blah...blah...blah", connection);
>>>>
>>>>
>>>>then simply call
>>>>
>>>>dataadapter.Update(your dataset);
>>>>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform