public bool insertDB(String sql)
        {
            using (SqlCommand cmd = new SqlCommand())       //这句代码是什么意思??做什么用的??
            {
                try
                {
                    cmd.CommandText = sql;                  //设置SQL语句
                    cmd.Connection = ConDB();               //调用打开数据库连接方法
                    cmd.ExecuteNonQuery();                  //执行
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

解决方案 »

  1.   

    楼上正解,除了定义范围,和在结束时处理对象它还有其它两种用法
    1、using指令。using+命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间
    2、using别名。using+别名 = 包括详细命名空间信息的具体的类型。
      

  2.   

    using (SqlCommand cmd = new SqlCommand())    定义和设置作用范围  
      

  3.   

    using (SqlCommand cmd = new SqlCommand())////上面的代码在编译后大概会变成下面的样子吧
    SqlCommand cmd = new SqlCommand();
    try{
        .....
    }catch{}
    finally{
        cmd.dispose();
    }
      

  4.   

    using () 里头的类必须实现 IDispose结果是在 {} 结束的时候自动调用 Dispose();是 C# 的语法糖, 翻译为托管码就是楼上说的那样
      

  5.   

    主要是出了{},他会帮助你收垃圾,即Dispose()
      

  6.   

    using (SqlCommand cmd = new SqlCommand())
      using 引用非托管资源
        像数据连接对象,IO之类的非托管资源并不能在.net的托管来释放资源,所以使用using 指令能够告诉.net运行库,此资源是非托管,用完后要调用Dispose()释放资源
      

  7.   

    我的理解是:
    using()
    {
       //do somethings
    }//释放资源 dispose与Delphi的with用法类似
    例:
    with XXX do
    begin
    //do something
    end