How.. Net executes Multiple SQL statements at a time. Generally, only one SQL statement can be executed at a time unless a stored procedure is used. For example, the following services are available: when deleting a user (assuming userid = 5), you also need to delete all posts and replies from the user on the Forum (to ensure the integrity of the reference ), assume that the user table is users, the post table is articles (with The USERID field), and the reply table is replys (with The USERID field ), generally, there are three steps: (1) Delete all replies with userid = 5 in the replys table; (2) Delete All posts with userid = 5 in the articles table; (3) delete users whose userid is 5 in the users table.
In each step above, create an oracleconnection, create an oraclecommand, executenonquery () method, then oraclecommand call dispose () method, and finally oracleconnection call the close () method. This process is executed in the above process. We know that database connection operations are time-consuming. Is there any better way? The answer is yes.
PS/SQL is used. Begin
Delete From Replys Where Userid = 5 ; -- Delete replies
Delete From Articles Where Userid = 5 ; -- Delete post
Delete From Users Where Userid = 5 ; -- Delete a user
End ;
The entire process can be written as follows: Private Void Deleteuser ( Int Userid)
{
String Deletesql = " Begin Delete from replys where userid = {0}; delete from articles where userid = {1}; delete from users where userid = {2}; end; " ;
Deletesql = String. Format (deletesql, userid );
Oracleconnection connection = New Oracleconnection (connectionstring );
Connection. open ();
Oraclecommand cmd = New Oraclecommand (deletesql, connection );
Cmd. Dispose ();
Connection. Close ();
}
In this way, all the operations can be completed in one connection. Of course, thisCodeTransaction processing is not considered. In actual use, you may consider adding as needed.