小弟刚刚开始学asp.net,碰到下面一段语句.
public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms) { SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connString)) {
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}不明白using (SqlConnection conn = new SqlConnection(connString))的作用,希望高手做答

解决方案 »

  1.   

    在 using 语句中创建一个实例,确保退出 using 语句时在对象上调用 Dispose。当到达 using 语句的末尾,或者如果在语句结束之前引发异常并且控制离开语句块,都可以退出 using 语句。实例化的对象必须实现 System.IDisposable 接口。
      

  2.   

    楼上的高手能说得再详细一点么?我在MSDN上面似乎找不到这方面的资料
      

  3.   

    using 
    {
    }
    等同于
    try
    {
    }
    catch(Exception ex)
    {       
      throw ex;
    }
      

  4.   

    OK,基本明白了.MSDN上查到的:You create an instance in a using statement to ensure that Dispose is called on the object when the using statement is exited. A using statement can be exited either when the end of the using statement is reached or if, for example, an exception is thrown and control leaves the statement block before the end of the statement.The object you instantiate must implement the System.IDisposable interface.2楼翻成中文了...谢谢各位高手,接分