When csla implements a nested database connection, use a database connection to view the instance:
Code 1 private string _ conn = "connection string ";
2 Private void inserta ()
3 {
4 using (var ctx = connectionmanager <sqlconnection>. getmanager (_ Conn, false ))
5 {
6 using (var cm = CTX. Connection. createcommand ())
7 {
8 cm. commandtype = commandtype. text;
9 cm. commandtext = "insert into a values ('01 ')";
10 cm. executenonquery ();
11}
12 insertb ();
13}
14}
15
16 private void insertb ()
17 {
18 using (var ctx = connectionmanager <sqlconnection>. getmanager (_ Conn, false ))
19 {
20 using (var cm = CTX. Connection. createcommand ())
21 {
22 cm. commandtype = commandtype. text;
23 cm. commandtext = "insert into B values ('02 ')";
24 cm. executenonquery ();
25}
26}
27}
When you create a connection in the insertb method, the connection created in inserta is used.
Implementation principle:
1. Create a connection Mgr (connection variable name) in inserta, save the connection string in the local context localcontext, and set the counter mrefcount ++ to 1,
2. If this connection string is found in localcontext when you create a connection in insertb, use the memory of the existing connection Mgr. The counter mrefcount ++ is 2.
3. When the using block in insertb ends, the counter mrefcount -- is 1
4. When the using block in inserta ends, the counter mrefcount -- is 0, the database connection is destroyed, and the connection string is removed from localcontext.