There is such a need. There is a table in the database. When keeping a record each time, you must first determine whether the record exists (based on whether the values of some columns are equal) to choose update) or add (insert). When using subsonic, the system tries to read data based on the condition column and then saves the data with Save. The system determines whether to use update or insert, as shown below:Code:
Collectioncompany Company = new collectioncompany ();
Company. loadandclosereader (collectioncompany. Query (). Where ("column 1", value1). And ("column 2", value2). executereader ());
// If isloaded is successfully loaded, isnew is set to false.
// When isnew is false and save () is called, the SQL statement generated is update, and the insert statement is vice versa.
// In addition, if isnew is false, the generated update statement is as follows:
// Update collectioncompany set xxx = 'xxx', YYY = 'yyy'... where id = 1
// The updated column is the column in dirtycolumns.
If (! Company. isloaded ){
Company. ID = getmaxid (); // The database does not exist-New Record, generate a new ID.
}
Company. Title = "XXXX ";
Company. Tel = "XXXX ";
// Other columns...
Company. Save ();
GenerallyProgramThe code is similar to the code given above. In these cases, you do not need to call markold () or marknew () by yourself ()
However, in some applications, you may need to manually call these methods. Note the following:
Note that after a new entity class, for example, collectioncompany Company = new collectioncompany. before markold, no paid value for company attributes (columns) will be added to dirtycolumns. Therefore, if you operate the attributes first, then call company. when you call the markold () method. when saving (), you will find that there is no specific column error after the update set, the company. the loadandclosereader method automatically calls company when the read is successful. markold (). Therefore, the SAVE () method below corresponds to the update method, and the columns after the update set are the columns updated after the load method (including the title, tel, etc)
Recently, I encountered this problem when I was working on a collection program. I used SQL 2000, vs2005, and subsonic To Do This. Generally, these programs can be simple and easy to do, saving the trouble, however, the collection rules may change, so we put the collection rules in an independent project and do not intend to put the database operation code in this project, however, in this project, the corresponding class (such as collectioncompany) of the table name generated by subsonic is used as the object class. Of course, it is best to leave the object class with a state, however, the same table name class generated by subsonic is not a pure entity class. when I judge in winform that the record already exists, I will use the ID of the existing record and pay the value to the object class returned by the collection rule project class () there is no column after the update set. The solution is to immediately call the markold method for the newly created class in the collection rules, for example:
Collectioncompany Company = new collectioncompany ()
Company. markold ();
.......
Return company;
The problem is that some unnecessary columns (columns used for condition judgment) will also be behind the update set, but you do not need to call the load method in the collection rule class, (Now, why not call it? Maybe I didn't want to call it at the time, haha)