标签: using

在处理命令并且直接在命令上定义连接时,连接是否关闭?

我知道很多例子都存在,其中定义了SqlConnection,然后在Using块中定义了SqlCommand:

using (var conn = new SqlConnection(connString)) {
      using (var cmd = new SqlCommand()) {
        cmd.Connection = conn;
        //open the connection
      }
}
Run Code Online (Sandbox Code Playgroud)

我的问题:如果我直接在SqlCommand上定义连接,那么当命令被释放时连接是否会关闭?

using (var cmd = new SqlCommand()) {
      cmd.Connection = new SqlConnection(connString);
      //open the connection
}
Run Code Online (Sandbox Code Playgroud)

c# using using-statement

8
推荐指数
3
解决办法
5005
查看次数

python是否有类似C++的关键字?

在C++中,您通常可以通过仔细使用"using"关键字来大幅提高代码的可读性,例如:

void foo()
{
    std::vector< std::map <int, std::string> > crazyVector;
    std::cout << crazyVector[0].begin()->first;
}
Run Code Online (Sandbox Code Playgroud)

void foo()
{
    using namespace std; // limited in scope to foo
    vector< map <int, string> > crazyVector;
    cout << crazyVector[0].begin()->first;
}
Run Code Online (Sandbox Code Playgroud)

python是否存在类似的东西,或者我是否必须完全限定所有内容?

我将添加免责声明,我知道使用有其缺陷,应该适当限制范围.

python namespaces using

8
推荐指数
3
解决办法
9369
查看次数

我们是否需要在使用块中关闭C#BinaryWriter或BinaryReader?

有这个代码:

using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create)))
{
   //save something here
}
Run Code Online (Sandbox Code Playgroud)

我们需要关闭BinaryWriter吗?如果没有,为什么?

c# idisposable using

8
推荐指数
2
解决办法
5378
查看次数

为什么这个Using()给我一个错误?

我正在尝试打开一个(实际上有数百个)excel文件.我打开应用程序但想要在我打开的每个工作簿周围使用Using()功能.为什么会导致错误?

using (Excel.Workbook wbXL = appXL.Workbooks.Open(_sourceFullPath, Type.Missing, Excel.XlFileAccess.xlReadOnly))
{
    //stuff with wbXL
}
Run Code Online (Sandbox Code Playgroud)

使用获取红色下划线并说"'Microsoft.Office.Interop.excel.Workbook':在using语句中使用的类型必须可以隐式转换为'System.IDisposable'.

如何使这项工作?

c# excel interop using

8
推荐指数
1
解决办法
4万
查看次数

C#IDisposable使用:最佳实践

我已经在USING语句中包装我的OracleConnection和OracleCommand对象一段时间了,但是,在运行代码分析器之后,我发现OracleParameter也实现了IDisposable.以下代码是否正确?是否有更好的可读性或结构技术?乍一看,它似乎与USING语句混杂在一起:

using (OracleConnection conn = new OracleConnection(connectionstring))
{
    conn.Open();
    using (OracleCommand cmd = new OracleCommand(sql, conn))
    {
        cmd.BindByName = true;

        using (OracleParameter param1 = new OracleParameter("p1", OracleDbType.Int32, System.Data.ParameterDirection.Input))
        {
            param1.Value = int.Parse(value1);
            cmd.Parameters.Add(param1);
        }

        using (OracleParameter param2 = new OracleParameter("p2", OracleDbType.Varchar2, System.Data.ParameterDirection.Input))
        {
            param2.Value = value2;
            cmd.Parameters.Add(param2);
        }

        using (OracleDataReader dr = cmd.ExecuteReader())
        {
            // loop data here...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# coding-style idisposable using

8
推荐指数
1
解决办法
2960
查看次数

如何为枚举类编写“使用”语句?

在我写的石头,剪刀,剪刀程序中,我列举了三种不同的动作并将它们声明为一个类。但是,当我尝试编写using语句以便避免使用范围运算符时,它似乎不起作用。有人知道为什么吗?

enum class choice {rock, paper, scissors};

using namespace choice;

出现一条错误消息,说:[错误]'选择'不是名称空间名称。为什么是这样?我认为在这种情况下可以选择一个名称空间。

c++ enumeration namespaces using class

8
推荐指数
3
解决办法
4315
查看次数

AngularJS如何使用route删除IE9中的#符号

我无法删除IE9中的#符号.我搜索了一个答案,但没有找到解决办法.

这总是重定向到

http://myhost.com:8080/#/website/
Run Code Online (Sandbox Code Playgroud)

并显示此描述:

The requested resource is not available.
Run Code Online (Sandbox Code Playgroud)

locationprovider.html5mode(true)不管用.同样的路线在FireFox中工作并显示

http://myhost.com:8080/website/
Run Code Online (Sandbox Code Playgroud)

我怎么能纠正这个?

routes using internet-explorer-9 angularjs

8
推荐指数
2
解决办法
7695
查看次数

XamlParseException:无法加载文件或程序集"ResourceLibrary,..."或其依赖项之一.该系统找不到指定的文件

System.Windows.Markup.XamlParseExceptionoccurred

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '21' and line position '6'.

"Could not load file or assembly 'ResourceLibrary, Culture=neutral' or one of its dependencies. The system cannot find the file specified.":"ResourceLibrary, Culture=neutral"

=== Pre-bind state information ===
LOG: DisplayName = ResourceLibrary, Culture=neutral
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: ResourceLibrary, Culture=neutral | Domain ID: 2
WRN: A partial bind …
Run Code Online (Sandbox Code Playgroud)

c# wpf using project-reference visual-studio

8
推荐指数
1
解决办法
6032
查看次数

在具有临时基的派生类中对类型别名模板使用声明

如果基类依赖于模板参数,则不会在非限定名称查找中检查其范围。我可以使用该using声明从基类中引入名称。假设现在我在基类中有一个类型别名模板。可以使用该using声明将其引入派生类吗?

template<class T>
struct Base
{
    using Type1 = int;

    template<typename S>
    using Type2 = S;
};

template<class T>
struct Derived : Base<T>
{
    using typename Base<T>::Type1;           // Fine
    //using Type1 = typename Base<T>::Type1; // Also fine

    template<typename S>
    using Type2 = typename Base<T>::template Type2<S>;
};
Run Code Online (Sandbox Code Playgroud)

可以Type2用类似于(未注释)的行替换行Type1吗?

c++ templates using c++11

8
推荐指数
1
解决办法
360
查看次数

带有一次性成员的C#ValueTuple

假设我有一个方法foo可以返回ValueTuple其中一个成员是一次性的,例如(IDisposable, int).

确保返回的一次性物体在呼叫方面正确配置的最佳方法是什么?

我尝试了以下方法:

using (var (disposable, number) = foo())
{
    // do some stuff using disposable and number
}
Run Code Online (Sandbox Code Playgroud)

但这不会编译:

'(IDisposable disposable,int number)':using语句中使用的类型必须可以隐式转换为'System.IDisposable'

我是否真的需要将我的代码包装在try-finally块中并将我的对象显式地放在finally块中,或者有更好的方法吗?

实际上我希望看​​到新的ValueTuples实现IDisposable接口并调用Dispose()他们所有的一次性成员.这可能是微软有价值的功能要求吗?

c# idisposable using valuetuple

8
推荐指数
1
解决办法
650
查看次数