For processing purposes, update is performed when there is a target record in the datasheet, insert is executed when the target record does not exist in the data table, and the process of querying the database is reduced once
The stored procedure is designed as follows:
CREATE PROCEDURE ' Pro_savedata ' (in ' sinst ' varchar (+), in ' SUPDT ' varchar ") begin# Direct update record set @v_updsql =SUPDT; prepare stmt from @v_updsql; EXECUTE stmt; #记录不存在, execute insertif row_count () =0 thenset @v_intsql =sinst; Prepare stmt from @v_intsql; EXECUTE stmt; END If;deallocate prepare stmt; END;
C # calls are as follows:
int r = data. ExecuteNonQuery (System.Data.CommandType.StoredProcedure, @ "Call Pro_savedata (' INSERT into ' table ' VALUES (' 1 ', ' Username ') ', ' UPDATE table SET name= ' table222 ' WHERE id= ' 1 '; ') ', null);
MySQL simplifies insert and update through stored procedures