
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
	<head><meta forua="true" http-equiv="Cache-Control" content="max-age=0" /></head><card id="MainCard" title="&#x6B22;&#x8FCE;&#x5149;&#x4E34;"><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a><br/>&nbsp;</p><p><b>&#x6807;&#x9898;&#x3A;</b> &#x45;&#x66;&#x66;&#x65;&#x63;&#x74;&#x69;&#x76;&#x65;&#x20;&#x43;&#x23;&#x20;&#x539F;&#x5219;&#x31;&#x35;&#xFF1A;&#x4F7F;&#x7528;&#x75;&#x73;&#x69;&#x6E;&#x67;&#x548C;&#x74;&#x72;&#x79;&#x2F;&#x66;&#x69;&#x6E;&#x61;&#x6C;&#x6C;&#x79;&#x6765;&#x505A;&#x8D44;&#x6E90;&#x6E05;&#x7406;</p><p><b>&#x4F5C;&#x8005;&#x3A;</b> &#x64;&#x6E;&#x61;&#x77;&#x6F;</p><p><b>&#x65E5;&#x671F;&#x3A;</b> &#x32;&#x30;&#x30;&#x39;&#x2D;&#x30;&#x37;&#x2D;&#x33;&#x31;&#x20;&#x30;&#x39;&#x3A;&#x35;&#x32;&#x20;&#x41;&#x4D;</p><p><b>&#x5206;&#x7C7B;&#x3A;</b> <a href="wap.asp?do=showLog&amp;cateID=5">&#x57;&#x65;&#x62;&#x7F16;&#x7A0B;</a></p><p><b>&#x5185;&#x5BB9;&#x3A;</b> &#x4F7F;&#x7528;&#x975E;&#x6258;&#x7BA1;&#x8D44;&#x6E90;&#x7684;&#x7C7B;&#x578B;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;IDisposable&#x63A5;&#x53E3;&#x7684;Dispose()&#x65B9;&#x6CD5;&#x6765;&#x7CBE;&#x786E;&#x7684;&#x91CA;&#x653E;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x3002;.Net&#x73AF;&#x5883;&#x7684;&#x8FD9;&#x4E00;&#x89C4;&#x5219;&#x4F7F;&#x5F97;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x4EE3;&#x7801;&#x7684;&#x804C;&#x8D23;&#x662F;&#x7C7B;&#x578B;&#x7684;&#x4F7F;&#x7528;&#x8005;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7C7B;&#x578B;&#x6216;&#x7CFB;&#x7EDF;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#x4F60;&#x5728;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x6709;Dispose()&#x65B9;&#x6CD5;&#x7684;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x4F60;&#x5C31;&#x6709;&#x8D23;&#x4EFB;&#x6765;&#x8C03;&#x7528;Dispose()&#x65B9;&#x6CD5;&#x6765;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;&#x6700;&#x597D;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x4FDD;&#x8BC1;Dispose()&#x88AB;&#x8C03;&#x7528;&#x7684;&#x7ED3;&#x6784;&#x662F;&#x4F7F;&#x7528;using&#x8BED;&#x53E5;&#x6216;&#x8005;try/finally&#x5757;&#x3002;<br/>&#x6240;&#x6709;&#x5305;&#x542B;&#x975E;&#x6258;&#x7BA1;&#x8D44;&#x6E90;&#x7684;&#x7C7B;&#x578B;&#x5E94;&#x8BE5;&#x5B9E;&#x73B0;IDisposable&#x63A5;&#x53E3;&#xFF0C;&#x53E6;&#x5916;&#xFF0C;&#x5F53;&#x4F60;&#x5FD8;&#x8BB0;&#x6070;&#x5F53;&#x7684;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5B83;&#x4EEC;&#x4F1A;&#x88AB;&#x52A8;&#x7684;&#x521B;&#x5EFA;&#x6790;&#x6784;&#x51FD;&#x6570;&#x3002;&#x5982;&#x679C;&#x4F60;&#x5FD8;&#x8BB0;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E9B;&#x975E;&#x5185;&#x5B58;&#x8D44;&#x6E90;&#x4F1A;&#x5728;&#x665A;&#x4E9B;&#x65F6;&#x5019;&#xFF0C;&#x6790;&#x6784;&#x51FD;&#x6570;&#x88AB;&#x786E;&#x5207;&#x8C03;&#x7528;&#x65F6;&#x5F97;&#x5230;&#x91CA;&#x653E;&#x3002;&#x8FD9;&#x5C31;&#x4F7F;&#x5F97;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#x5728;&#x5185;&#x5B58;&#x65F6;&#x5F85;&#x7684;&#x65F6;&#x95F4;&#x66F4;&#x957F;&#xFF0C;&#x4ECE;&#x800C;&#x4F1A;&#x4F7F;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4F1A;&#x56E0;&#x7CFB;&#x7EDF;&#x8D44;&#x6E90;&#x5360;&#x7528;&#x592A;&#x591A;&#x800C;&#x901F;&#x5EA6;&#x4E0B;&#x964D;&#x3002;<br/>&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;C#&#x8BED;&#x8A00;&#x7684;&#x8BBE;&#x8BA1;&#x8005;&#x7CBE;&#x786E;&#x7684;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x662F;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x4ED6;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x6765;&#x4F7F;&#x8FD9;&#x53D8;&#x5F97;&#x7B80;&#x5355;&#x4E86;&#x3002;<br/>&#x5047;&#x8BBE;&#x4F60;&#x5199;&#x4E86;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>}<br/>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#x7684;&#x4E24;&#x4E2A;&#x53EF;&#x5904;&#x7406;&#x5BF9;&#x8C61;&#x6CA1;&#x6709;&#x88AB;&#x6070;&#x5F53;&#x7684;&#x91CA;&#x653E;&#xFF1A;SqlConnection&#x548C;SqlCommand&#x3002;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x540C;&#x65F6;&#x4FDD;&#x5B58;&#x5728;&#x5185;&#x5B58;&#x91CC;&#x76F4;&#x5230;&#x6790;&#x6784;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x3002;(&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x90FD;&#x662F;&#x4ECE;System.ComponentModel.Component&#x7EE7;&#x627F;&#x6765;&#x7684;&#x3002;)<br/>&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x7684;&#x65B9;&#x6CD5;&#x5C31;&#x662F;&#x5728;&#x4F7F;&#x7528;&#x5B8C;&#x547D;&#x4EE4;&#x548C;&#x94FE;&#x63A5;&#x540E;&#x5C31;&#x8C03;&#x7528;&#x5B83;&#x4EEC;&#x7684;Dispose:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;mySqlCommand.Dispose( );<br/>&nbsp;&nbsp;myConnection.Dispose( );<br/>}<br/>&#x8FD9;&#x5F88;&#x597D;&#xFF0C;&#x9664;&#x975E;SQL&#x547D;&#x4EE4;&#x5728;&#x6267;&#x884C;&#x65F6;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x8FD9;&#x65F6;&#x4F60;&#x7684;Dispose()&#x8C03;&#x7528;&#x5C31;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x6210;&#x529F;&#x3002;using&#x8BED;&#x53E5;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;Dispose()&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x5F53;&#x4F60;&#x628A;&#x5BF9;&#x8C61;&#x5206;&#x914D;&#x5230;using&#x8BED;&#x53E5;&#x5185;&#x65F6;&#xFF0C;C&#xFF03;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x5C31;&#x628A;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#x653E;&#x5230;&#x4E00;&#x4E2A;try/finally&#x5757;&#x5185;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;using ( SqlConnection myConnection = new<br/>&nbsp;&nbsp;&nbsp;&nbsp;SqlConnection( connString ))<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;using ( SqlCommand mySqlCommand = new<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>}<br/>&#x5F53;&#x4F60;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5185;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53EF;&#x5904;&#x7406;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;using&#x8BED;&#x53E5;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x4FDD;&#x8BC1;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x88AB;&#x6070;&#x5F53;&#x7684;&#x5904;&#x7406;&#x6389;&#x3002;&#x5F53;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#x88AB;&#x5206;&#x914D;&#x65F6;&#xFF0C;&#x4F1A;&#x88AB;&#x7F16;&#x8BD1;&#x5668;&#x653E;&#x5230;&#x4E00;&#x4E2A;try/finally&#x5757;&#x4E2D;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;&#x7F16;&#x8BD1;&#x6210;&#x7684;IL&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;SqlConnection myConnection = null;<br/>// Example Using clause:<br/>using ( myConnection = new SqlConnection( connString ))<br/>{<br/>&nbsp;&nbsp;myConnection.Open();<br/>}<br/>// example Try / Catch block:<br/>try {<br/>&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;myConnection.Open();<br/>}<br/>finally {<br/>&nbsp;&nbsp;myConnection.Dispose( );<br/>}<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x5C31;&#x6211;&#x4E2A;&#x4EBA;&#x5BF9;try/catch/finally&#x5757;&#x7684;&#x4F7F;&#x7528;&#x7ECF;&#x9A8C;&#x800C;&#x8A00;&#xFF0C;&#x6211;&#x89C9;&#x5F97;&#x4E0A;&#x9762;&#x8FD9;&#x6837;&#x7684;&#x505A;&#x6CD5;&#x975E;&#x5E38;&#x4E0D;&#x65B9;&#x4FBF;&#x3002;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x8D44;&#x6E90;&#x5F97;&#x5230;&#x91CA;&#x653E;&#xFF0C;&#x5374;&#x65E0;&#x6CD5;&#x53D1;&#x73B0;&#x9519;&#x8BEF;&#x3002;&#x5173;&#x4E8E;&#x5982;&#x4F55;&#x540C;&#x65F6;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x53C8;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x7684;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x53C2;&#x8003;&#x4E00;&#x4E0B;&#x5176;&#x5B83;&#x76F8;&#x5173;&#x8D44;&#x6E90;&#xFF0C;&#x5982;Jeffrey&#x7684;.Net&#x6846;&#x67B6;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;,&#x4FEE;&#x8BA2;&#x7248;)<br/>&#x5982;&#x679C;&#x4F60;&#x628A;&#x4E00;&#x4E2A;&#x4E0D;&#x80FD;&#x5904;&#x7406;&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#x653E;&#x7F6E;&#x5728;using&#x8BED;&#x53E5;&#x5185;&#xFF0C;C&#xFF03;&#x7F16;&#x8BD1;&#x5668;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Does not compile:<br/>// String is sealed, and does not support IDisposable.<br/>using( string msg = &#34;This is a message&#34; )<br/>&nbsp;&nbsp;Console.WriteLine( msg );<br/>using&#x53EA;&#x80FD;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#xFF0C;&#x90A3;&#x4E9B;&#x652F;&#x6301;IDispose&#x63A5;&#x53E3;&#x7684;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#xFF0C;&#x5E76;&#x4E0D;&#x662F;&#x4EFB;&#x610F;&#x7684;&#x5BF9;&#x8C61;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Does not compile.<br/>// Object does not support IDisposable.<br/>using ( object obj = Factory.Cr&#101;ateResource( ))<br/>&nbsp;&nbsp;Console.WriteLine( obj.ToString( ));<br/>&#x5982;&#x679C;obj&#x5B9E;&#x73B0;&#x4E86;IDispose&#x63A5;&#x53E3;&#xFF0C;&#x90A3;&#x4E48;using&#x8BED;&#x53E5;&#x5C31;&#x4F1A;&#x751F;&#x6210;&#x8D44;&#x6E90;&#x6E05;&#x7406;&#x4EE3;&#x7801;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;using&#x5C31;&#x9000;&#x5316;&#x6210;&#x4F7F;&#x7528;using(null)&#xFF0C;&#x8FD9;&#x662F;&#x5B89;&#x5168;&#x7684;&#xFF0C;&#x4F46;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4F5C;&#x7528;&#x3002;&#x5982;&#x679C;&#x4F60;&#x5BF9;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x5E94;&#x8BE5;&#x653E;&#x5728;using&#x8BED;&#x53E5;&#x4E2D;&#x4E0D;&#x662F;&#x5F88;&#x786E;&#x5B9A;&#xFF0C;&#x5B81;&#x53EF;&#x4E3A;&#x4E86;&#x66F4;&#x5B89;&#x5168;&#xFF1A;&#x5047;&#x8BBE;&#x8981;&#x8FD9;&#x6837;&#x505A;&#xFF0C;&#x800C;&#x4E14;&#x6309;&#x524D;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x628A;&#x5B83;&#x653E;&#x5230;using&#x8BED;&#x53E5;&#x4E2D;&#x3002;<br/>&#x8FD9;&#x91CC;&#x8BB2;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x60C5;&#x51B5;&#xFF1A;&#x65E0;&#x8BBA;&#x4F55;&#x65F6;&#xFF0C;&#x5F53;&#x4F60;&#x5728;&#x67D0;&#x4E2A;&#x65B9;&#x6CD5;&#x5185;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53EF;&#x5904;&#x7406;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x628A;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x653E;&#x5728;using&#x8BED;&#x53E5;&#x5185;&#x3002;&#x73B0;&#x5728;&#x4F60;&#x5B66;&#x4E60;&#x4E00;&#x4E9B;&#x66F4;&#x590D;&#x6742;&#x7684;&#x5E94;&#x7528;&#x3002;&#x8FD8;&#x662F;&#x524D;&#x9762;&#x90A3;&#x4E2A;&#x4F8B;&#x5B50;&#x91CC;&#x987B;&#x8981;&#x91CA;&#x653E;&#x7684;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#xFF1A;&#x94FE;&#x63A5;&#x548C;&#x547D;&#x4EE4;&#x3002;&#x524D;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x544A;&#x8BC9;&#x4F60;&#x521B;&#x5EFA;&#x4E86;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;using&#x8BED;&#x53E5;&#xFF0C;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x53EF;&#x5904;&#x7406;&#x5BF9;&#x8C61;&#x3002;&#x6BCF;&#x4E2A;using&#x8BED;&#x53E5;&#x5C31;&#x751F;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;try/finally&#x5757;&#x3002;&#x7B49;&#x6548;&#x7684;&#x4F60;&#x5199;&#x4E86;&#x8FD9;&#x6837;&#x7684;&#x4EE3;&#x7801;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = null;<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = null;<br/>&nbsp;&nbsp;try<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;&nbsp;&nbsp;try<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( mySqlCommand != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.Dispose( );<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( myConnection != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Dispose( );<br/>&nbsp;&nbsp;}<br/>}<br/>&#x6BCF;&#x4E00;&#x4E2A;using&#x8BED;&#x53E5;&#x751F;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5D4C;&#x5957;&#x7684;try/finally&#x5757;&#x3002;&#x6211;&#x53D1;&#x73B0;&#x8FD9;&#x662F;&#x5F88;&#x7CDF;&#x7CD5;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x6240;&#x4EE5;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x9047;&#x5230;&#x591A;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86;IDisposable&#x63A5;&#x53E3;&#x7684;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x6211;&#x66F4;&#x613F;&#x610F;&#x5199;&#x81EA;&#x5DF1;&#x7684;try/finally&#x5757;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = null;<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = null;<br/>&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( mySqlCommand != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.Dispose();<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( myConnection != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Dispose();<br/>&nbsp;&nbsp;}<br/>}<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x4F5C;&#x8005;&#x91CC;&#x7684;&#x5224;&#x65AD;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x4E3A;null&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;&#xFF0C;&#x7279;&#x522B;&#x662F;&#x4E00;&#x4E9B;&#x5C01;&#x88C5;&#x4E86;COM&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x6709;&#x4E9B;&#x65F6;&#x5019;&#x7684;&#x91CA;&#x653E;&#x662F;&#x9690;&#x5F0F;&#x7684;&#xFF0C;&#x5F53;&#x4F60;&#x518D;&#x91CA;&#x653E;&#x4E00;&#x4E9B;&#x7A7A;&#x5BF9;&#x8C61;&#x65F6;&#x4F1A;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x3002;&#x4F8B;&#x5982;&#xFF1A;&#x540C;&#x4E00;&#x4E2A;COM&#x88AB;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x63A5;&#x53E3;&#x7684;&#x53D8;&#x91CF;&#x5F15;&#x7528;&#x65F6;&#xFF0C;&#x5728;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x4E0A;&#x8C03;&#x7528;&#x4E86;Dispose&#x540E;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x7684;&#x8C03;&#x7528;&#x5C31;&#x4F1A;&#x5931;&#x8D25;&#x3002;&#x5728;.Net&#x91CC;&#x4E5F;&#x8981;&#x6CE8;&#x610F;&#x8FD9;&#x6837;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x5224;&#x65AD;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x4E3A;null)<br/>&#x7136;&#x800C;&#xFF0C;&#x8BF7;&#x4E0D;&#x8981;&#x81EA;&#x4F5C;&#x806A;&#x660E;&#x8BD5;&#x56FE;&#x7528;as&#x6765;&#x5199;&#x8FD9;&#x6837;&#x7684;using&#x8BED;&#x53E5;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;// Bad idea. Potential resource leak lurks!<br/>&nbsp;&nbsp;SqlConnection myConnection =<br/>&nbsp;&nbsp;&nbsp;&nbsp;new SqlConnection( connString );<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using ( myConnection as IDisposable )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (mySqlCommand as IDisposable )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>}<br/>&#x8FD9;&#x770B;&#x4E0A;&#x53BB;&#x5F88;&#x6E05;&#x723D;&#xFF0C;&#x4F46;&#x6709;&#x4E00;&#x4E2A;&#x72E1;&#x733E;&#x7684;(subtle )&#x7684;bug&#x3002; &#x5982;&#x679C;SqlCommand()&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x90A3;&#x4E48;SqlConnection&#x5BF9;&#x8C61;&#x5C31;&#x4E0D;&#x53EF;&#x80FD;&#x88AB;&#x5904;&#x7406;&#x4E86;&#x3002;&#x4F60;&#x5FC5;&#x987B;&#x786E;&#x4FDD;&#x6BCF;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86;IDispose&#x63A5;&#x53E3;&#x7684;&#x5BF9;&#x8C61;&#x5206;&#x914D;&#x5728;&#x5728;using&#x8303;&#x56F4;&#x5185;&#xFF0C;&#x6216;&#x8005;&#x5728;try/finally&#x5757;&#x5185;&#x3002;&#x5426;&#x5219;&#x4F1A;&#x51FA;&#x73B0;&#x8D44;&#x6E90;&#x6CC4;&#x6F0F;&#x3002;<br/>&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C;&#x4F60;&#x5DF2;&#x7ECF;&#x5B66;&#x4F1A;&#x4E86;&#x4E24;&#x79CD;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x65E0;&#x8BBA;&#x4F55;&#x65F6;&#x5728;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x5185;&#x5904;&#x7406;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;using&#x8BED;&#x53E5;&#x662F;&#x6700;&#x597D;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x786E;&#x4FDD;&#x7533;&#x8BF7;&#x7684;&#x8D44;&#x6E90;&#x5728;&#x5404;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x90FD;&#x5F97;&#x5230;&#x91CA;&#x653E;&#x3002;&#x5F53;&#x4F60;&#x5728;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x91CC;&#x5206;&#x914D;&#x4E86;&#x591A;&#x4E2A;(&#x5B9E;&#x73B0;&#x4E86;IDisposable&#x63A5;&#x53E3;&#x7684;)&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x521B;&#x5EFA;&#x591A;&#x4E2A;using&#x5757;&#x6216;&#x8005;&#x4F7F;&#x7528;&#x4F60;&#x81EA;&#x5DF1;&#x7684;try/finally&#x5757;&#x3002;<br/>&#x5BF9;&#x53EF;&#x5904;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x7406;&#x89E3;&#x6709;&#x4E00;&#x70B9;&#x70B9;&#x7EC6;&#x5FAE;&#x7684;&#x533A;&#x522B;&#x3002;&#x6709;&#x4E00;&#x4E9B;&#x5BF9;&#x8C61;&#x540C;&#x65F6;&#x652F;&#x6301;Disponse&#x548C;Close&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x6765;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;SqlConnection&#x5C31;&#x662F;&#x5176;&#x4E2D;&#x4E4B;&#x4E00;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x50CF;&#x8FD9;&#x6837;&#x5173;&#x95ED;SqlConnection&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = null;<br/>&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( myConnection != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Close();<br/>&nbsp;&nbsp;}<br/>}<br/>&#x8FD9;&#x4E2A;&#x7248;&#x672C;&#x5173;&#x95ED;&#x4E86;&#x94FE;&#x63A5;&#xFF0C;&#x4F46;&#x5B83;&#x786E;&#x5B9E;&#x4E0E;&#x5904;&#x7406;&#x5BF9;&#x8C61;&#x662F;&#x4E0D;&#x4E00;&#x6837;&#x7684;&#x3002;Dispose&#x65B9;&#x6CD5;&#x4F1A;&#x91CA;&#x653E;&#x66F4;&#x591A;&#x7684;&#x8D44;&#x6E90;&#xFF0C;&#x5B83;&#x8FD8;&#x4F1A;&#x544A;&#x8BC9;GC&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5DF2;&#x7ECF;&#x4E0D;&#x518D;&#x987B;&#x8981;&#x6790;&#x6784;&#x4E86;(&#x8BD1;&#x6CE8;&#xFF1A;&#x5173;&#x4E8E;C#&#x91CC;&#x7684;&#x6790;&#x6784;&#xFF0C;&#x53EF;&#x4EE5;&#x53C2;&#x8003;&#x5176;&#x5B83;&#x65B9;&#x9762;&#x7684;&#x4E66;&#x7C4D;)&#x3002;Dispose&#x4F1A;&#x8C03;&#x7528;GC.SuppressFinalize()&#xFF0C;&#x4F46;Close()&#x4E00;&#x822C;&#x4E0D;&#x4F1A;&#x3002;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#xFF0C;&#x5BF9;&#x8C61;&#x4F1A;&#x5230;&#x6790;&#x6784;&#x961F;&#x5217;&#x4E2D;&#x6392;&#x961F;&#xFF0C;&#x5373;&#x4F7F;&#x6790;&#x6784;&#x5E76;&#x4E0D;&#x662F;&#x987B;&#x8981;&#x7684;&#x3002;&#x5F53;&#x4F60;&#x6709;&#x9009;&#x62E9;&#x65F6;&#xFF0C;Dispose()&#x6BD4;Colse()&#x8981;&#x597D;&#x3002;&#x4F60;&#x4F1A;&#x5728;&#x539F;&#x5219;18&#x91CC;&#x5B66;&#x4E60;&#x66F4;&#x66F4;&#x7CBE;&#x5F69;&#x7684;&#x5185;&#x5BB9;&#x3002;<br/>Dispose()&#x5E76;&#x4E0D;&#x4F1A;&#x4ECE;&#x5185;&#x5B58;&#x91CC;&#x628A;&#x5BF9;&#x8C61;&#x79FB;&#x8D70;&#xFF0C;&#x5BF9;&#x4E8E;&#x8BA9;&#x5BF9;&#x8C61;&#x91CA;&#x653E;&#x975E;&#x6258;&#x7BA1;&#x8D44;&#x6E90;&#x6765;&#x8BF4;&#x662F;&#x4E00;&#x4E2A;hook&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x8BF4;&#x4F60;&#x53EF;&#x80FD;&#x9047;&#x5230;&#x8FD9;&#x6837;&#x7684;&#x96BE;&#x9898;&#xFF0C;&#x5C31;&#x662F;&#x91CA;&#x653E;&#x4E00;&#x4E2A;&#x8FD8;&#x5728;&#x4F7F;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x4E0D;&#x8981;&#x91CA;&#x653E;&#x4E00;&#x4E2A;&#x5728;&#x7A0B;&#x5E8F;&#x5176;&#x5B83;&#x5730;&#x65B9;&#x8FD8;&#x5728;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x3002;<br/>&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;C#&#x91CC;&#x7684;&#x8D44;&#x6E90;&#x7BA1;&#x7406;&#x6BD4;C++&#x8FD8;&#x8981;&#x56F0;&#x96BE;&#x3002;&#x4F60;&#x4E0D;&#x80FD;&#x6307;&#x671B;&#x786E;&#x5B9A;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x6765;&#x6E05;&#x7406;&#x4F60;&#x6240;&#x4F7F;&#x7528;&#x7684;&#x6240;&#x6709;&#x8D44;&#x6E90;&#x3002;&#x4F46;&#x5783;&#x573E;&#x56DE;&#x6536;&#x5668;&#x5374;&#x8BA9;&#x4F60;&#x66F4;&#x8F7B;&#x677E;&#xFF0C;&#x4F60;&#x7684;&#x5927;&#x4ECE;&#x6570;&#x7C7B;&#x578B;&#x4E0D;&#x5FC5;&#x5B9E;&#x73B0;IDisposable&#x63A5;&#x53E3;&#x3002;&#x5728;.Net&#x6846;&#x67B6;&#x91CC;&#x7684;1500&#x591A;&#x4E2A;&#x7C7B;&#x4E2D;&#xFF0C;&#x53EA;&#x6709;&#x4E0D;&#x5230;100&#x4E2A;&#x7C7B;&#x5B9E;&#x73B0;&#x4E86;IDisposable&#x63A5;&#x53E3;&#x3002;&#x5F53;&#x4F60;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x4E86;IDisposeable&#x63A5;&#x53E3;&#x7684;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x8BB0;&#x5F97;&#x5728;&#x6240;&#x6709;&#x7684;&#x7C7B;&#x91CC;&#x90FD;&#x8981;&#x5904;&#x7406;&#x5B83;&#x4EEC;&#x3002;&#x4F60;&#x5E94;&#x8BE5;&#x628A;&#x5B83;&#x4EEC;&#x5305;&#x542B;&#x5728;using&#x8BED;&#x53E5;&#x4E2D;&#xFF0C;&#x6216;&#x8005;try/finally&#x5757;&#x4E2D;&#x3002;&#x4E0D;&#x7BA1;&#x7528;&#x54EA;&#x4E00;&#x79CD;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD;&#x6BCF;&#x65F6;&#x6BCF;&#x523B;&#x5BF9;&#x8C61;&#x90FD;&#x5F97;&#x5230;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x91CA;&#x653E;&#x3002;<br/>========================================================<br/>Item 15: Utilize using and TRy/finally for Resource Cleanup<br/>Types that use unmanaged system resources should be explicitly released using the Dispose() method of the IDisposable interface. The rules of the .NET environment make that the responsibility of the code that uses the type, not the responsibility of the type o&#114; the system. Therefore, anytime you use types that have a Dispose() method, it&#39;s your responsibility to release those resources by calling Dispose(). The best way to ensure that Dispose() always gets called is to utilize the using statement o&#114; a try/finally block. <br/>All types that own unmanaged resources implement the IDisposable interface. In addition, they defensively cr&#101;ate a finalizer for those times when you forget to dispose properly. If you forget to dispose of those items, those nonmemory resources are freed later, when finalizers get their chance to execute. All those objects then stay in memory that much longer, and your application becomes a slowly executing resource hog.<br/>Luckily for you, the C# language designers knew that explicitly releasing resources would be a common task. They added keywords to the language that make it easy.<br/>Suppose you wrote this code:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>}<br/>Two disposable objects are not properly cleaned up in this example: SqlConnection and SqlCommand. Both of these objects remain in memory until their finalizers are called. (Both of these classes inherit their finalizer from System.ComponentModel.Component.)<br/>You fix this problem by calling Dispose when you are finished with the command and the connection:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;mySqlCommand.Dispose( );<br/>&nbsp;&nbsp;myConnection.Dispose( );<br/>}<br/>That&#39;s fine, unless any exceptions get thrown while the SQL command executes. In that case, your calls to Dispose() never happen. The using statement ensures that Dispose() is called. You allocate an object inside a using statement, and the C# compiler generates a try/finally block around each object:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;using ( SqlConnection myConnection = new<br/>&nbsp;&nbsp;&nbsp;&nbsp;SqlConnection( connString ))<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;using ( SqlCommand mySqlCommand = new<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection ))<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>}<br/>Whenever you use one Disposable object in a function, the using clause is the simplest method to use to ensure that objects get disposed of properly. The using statement generates a TRy/finally block around the object being allocated. These two blocks generate exactly the same IL:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;SqlConnection myConnection = null;<br/>// Example Using clause:<br/>using ( myConnection = new SqlConnection( connString ))<br/>{<br/>&nbsp;&nbsp;myConnection.Open();<br/>}<br/>// example Try / Catch block:<br/>try {<br/>&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;myConnection.Open();<br/>}<br/>finally {<br/>&nbsp;&nbsp;myConnection.Dispose( );<br/>}<br/>If you use the using statement with a variable of a type that does not support the IDisposable interface, the C# compiler generates an error. For example:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Does not compile:<br/>// String is sealed, and does not support IDisposable.<br/>using( string msg = &#34;This is a message&#34; )<br/>&nbsp;&nbsp;Console.WriteLine( msg );<br/>The using statement works only if the compile-time type supports the IDisposable interface. You cannot use it with arbitrary objects:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// Does not compile.<br/>// Object does not support IDisposable.<br/>using ( object obj = Factory.Cr&#101;ateResource( ))<br/>&nbsp;&nbsp;Console.WriteLine( obj.ToString( ));<br/>A quick defensive as clause is all you need to safely dispose of objects that might o&#114; might not implement IDisposable:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;// The correct fix.<br/>// Object may o&#114; may not support IDisposable.<br/>object obj = Factory.Cr&#101;ateResource( );<br/>using ( obj as IDisposable )<br/>&nbsp;&nbsp;Console.WriteLine( obj.ToString( ));<br/>If obj implements IDisposable, the using statement generates the cleanup code. If not, the using statement degenerates to using(null), which is safe but doesn&#39;t do anything. If you&#39;re not sure whether you should wrap an object in a using block, err on the side of safety: Assume that it does and wrap it in the using clause shown earlier.<br/>That covers the simple case: Whenever you use one disposable object that is local to a method, wrap that one object in a using statement. Now you can look at a few more complicated usages. Two different objects need to be disposed in that first example: the connection and the command. The example I showed you cr&#101;ates two different using statements, one wrapping each of the two objects that need to be disposed. Each using statement generates a different try/finally block. In effect, you have written this construct:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = null;<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = null;<br/>&nbsp;&nbsp;try<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;&nbsp;&nbsp;try<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( mySqlCommand != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.Dispose( );<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( myConnection != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Dispose( );<br/>&nbsp;&nbsp;}<br/>}<br/>Every using statement cr&#101;ates a new nested TRy/finally block. I find that an ugly construct, so when I allocate multiple objects that implement IDisposable, I prefer to write my own try/finally blocks:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = null;<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = null;<br/>&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( mySqlCommand != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.Dispose();<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( myConnection != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Dispose();<br/>&nbsp;&nbsp;}<br/>}<br/>However, don&#39;t get too cute and try to build one using clause with as statements:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;// Bad idea. Potential resource leak lurks!<br/>&nbsp;&nbsp;SqlConnection myConnection =<br/>&nbsp;&nbsp;&nbsp;&nbsp;new SqlConnection( connString );<br/>&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using ( myConnection as IDisposable )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;using (mySqlCommand as IDisposable )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>}<br/>It looks cleaner, but it has a subtle bug. The SqlConnection object never gets disposed if the SqlCommand() constructor throws an exception. You must make sure that any objects that implement IDisposable are allocated inside the scope of a using block o&#114; a try block. Otherwise, resource leaks can occur.<br/>So far, you&#39;ve handled the two most obvious cases. Whenever you allocate one disposable object in a method, the using statement is the best way to ensure that the resources you&#39;ve allocated are freed in all cases. When you allocate multiple objects in the same method, cr&#101;ate multiple using blocks o&#114; write your own single try/finally block.<br/>There is one more nuance to freeing disposable objects. Some types support both a Dispose method and a Close method to free resources. SqlConnection is one of those classes. You could close SqlConnection like this:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void ExecuteCommand( string connString,<br/>&nbsp;&nbsp;string commandString )<br/>{<br/>&nbsp;&nbsp;SqlConnection myConnection = null;<br/>&nbsp;&nbsp;try {<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection = new SqlConnection( connString );<br/>&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand mySqlCommand = new SqlCommand( commandString,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection );<br/>&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Open();<br/>&nbsp;&nbsp;&nbsp;&nbsp;mySqlCommand.ExecuteNonQuery();<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;finally<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ( myConnection != null )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myConnection.Close();<br/>&nbsp;&nbsp;}<br/>}<br/>This version does close the connection, but that&#39;s not exactly the same as disposing of it. The Dispose method does more than free resources: It also notifies the Garbage Collector that the object no longer needs to be finalized. Dispose calls GC.SuppressFinalize(). Close typically does not. As a result, the object remains in the finalization queue, even though finalization is not needed. When you have the choice, Dispose() is better than Close(). You&#39;ll learn all the gory details in Item 18.<br/>Dispose() does not remove objects from memory. It is a hook to let objects release unmanaged resources. That means you can get into trouble by disposing of objects that are still in use. Do not dispose of objects that are still being referenced elsewh&#101;re in your program.<br/>In some ways, resource management can be more difficult in C# than it was in C++. You can&#39;t rely on deterministic finalization to clean up every resource you use. But a garbage-collected environment really is much simpler for you. The vast majority of the types you make use of do not implement IDisposable. Less than 100 classes in the .NET Framework implement IDisposablethat&#39;s out of more than 1,500 types. When you use the ones that do implement IDisposable, remember to dispose of them in all cases. You should wrap those objects in using clauses o&#114; TRy/finally blocks. Whichever you use, make sure that objects get disposed properly all the time, every time.</p><p> + <a href="#CommentCard">&#x67E5;&#x770B;&#x5F53;&#x524D;&#x65E5;&#x5FD7;&#x8BC4;&#x8BBA;</a> (0)</p><p>&nbsp;<br/><br/><a href="wap.asp?do=Login">&#x767B;&#x5F55;</a></p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.158&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card><card id="postCommentCard"><p><b>&#x6807;&#x9898;&#x3A;</b> <a href="#MainCard">&#x45;&#x66;&#x66;&#x65;&#x63;&#x74;&#x69;&#x76;&#x65;&#x20;&#x43;&#x23;&#x20;&#x539F;&#x5219;&#x31;&#x35;&#xFF1A;&#x4F7F;&#x7528;&#x75;&#x73;&#x69;&#x6E;&#x67;&#x548C;&#x74;&#x72;&#x79;&#x2F;&#x66;&#x69;&#x6E;&#x61;&#x6C;&#x6C;&#x79;&#x6765;&#x505A;&#x8D44;&#x6E90;&#x6E05;&#x7406;</a></p><p><br/>你没有权限发表评论</p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.158&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card><card id="CommentCard"><p>&#x6682;&#x65E0;&#x8BC4;&#x8BBA;</p><p><a href="#MainCard">&#x8FD4;&#x56DE;</a></p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.158&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
