How to update a data source
1. Command object
Update required attributes:
The connection includes the details of the data warehouse connection.
Commandtext command
Commandtype command type SQL character or stored procedure name
Text indicates the text string SQL
Tabledirect indicates the table name.
Storedprocedure indicates the name of the stored procedure.
A set of parameters objects.
2. dataadapter object
What is the difference between dataadapter and command?
> Command is mainly used to run commands.
> Dataadapter is mainly used to provide a storage space for multiple commands and provide two-way interaction between data warehouses and dataset.
Oh, a command object can only process queries, add, delete, or modify
Therefore, dataadapter uses four attributes to store four command objects.
The attributes are as follows: selectcommand, updatecommand, insertcommand, and deletecommand.
3. commandbuilder object
Oledbcommandbuilder objbuilder
Objbuilder = new oledbcommandbuilder (dataadapter)
Indicates where the command generator can obtain the selectcommand to create other commands.
Dataadapter. updatecommand = objbuilder. getupdatecommand ();
Dataadapter. insertcommand = objbuilder. getinsertcommand ();
Dataadapter. deletecommand = objbuilder. getdeletecommand ();
Note that in this case, selectcommand must contain a primary key field
4. dataadapter. Update ()
Dataadapter. Update (dataset, "tablesname ");
For exampleCodeMake sure that the deleted rows in the table are processed first, updated rows are processed, and inserted rows are processed.
[C #]
Datatable updtable = custds. Tables ["MERs"];
// First process deletes.
Custda. Update (updtable. Select (null, null, dataviewrowstate. Deleted ));
// Next process updates.
Custda. Update (updtable. Select (null, null, dataviewrowstate. modifiedcurrent ));
// Finally, process inserts.
Custda. Update (updtable. Select (null, null, dataviewrowstate. Added ));
The operation attributes of dataviewrowstate data view include deleted, modifiedcurrent, added, and unchanged.
Now, data warehouse update is complete.
[2003-05-28]
Use stored procedures
A stored procedure is similar to a function in code. It is stored on a data server and has a name.
Why use stored procedures?
1. huge and complex SQL statement impactProgramCode Reading
2. The Stored Procedure processed by the database server is faster and more efficient than using SQL statements directly.
To use a stored procedure, note that commandtype is set to storedproccess commandtext as the name of the stored procedure.
Eg: objcmd. commandtext = "[sales by category]";
Objcmd. commandtype = commandtype. storeprocedure;
Use XML
Since the design of ado.net takes XML into account, it processes XML data like the data from a database.
1. Write the XML file
objadapter. fill (objdataset, "employees"); // fill in the result set
objdataset. writexml (server. mappath ("employees.
Note:
1. The writexml () method of dataset is used first, extract information from dataset and format XML
2, server. mappath () indicates the path of the generated file, pointing to the current application directory
2. Read the XML file
objdataset. readxml (server. mappath ("employees. XML ");
3. Convert XML to a string
string strxml, strschema
strxml = objdataset. getxml ()
strschema = objdataset. getxmlschema ()
4. Once the XML is read into the dataset, it is no different from the data read from the database. You can also perform any of the preceding
operations, in the end, you only need to write the result set dataset of the operation to XML or database.