【问题标题】:Using SqlCommand Object in VB.NET在 VB.NET 中使用 SqlCommand 对象
【发布时间】:2009-11-05 09:27:13
【问题描述】:

我可以在 VB.NET 的一个过程中使用两个命令对象和一个打开的连接吗?

【问题讨论】:

  • 是的。您可以在一个 Open 连接中使用两个或多个 SqlCommand 对象。
  • 但是不能用一个Command对象打开两个DataReader。
  • 实际上,如果您在连接字符串中设置 MultipleActiveResultSets=true,您可以使用一个 Command 对象打开两个(或更多)DataReader。但是,使用 MARS 的性能可能比仅使用每个 DataReader 一个连接的“正常方式”要慢,您的里程可能会有所不同。

标签: vb.net sqlcommand


【解决方案1】:

是的,你可以。只要不关闭命令之间的连接,就可以正常工作。

这是一个 C# 示例,但我相信你可以解决它:

    using (SqlConnection cn = new SqlConnection("Connection String"))
    {

        SqlCommand cmd1 = new SqlCommand("Command1", cn);
        cmd1.CommandType = CommandType.StoredProcedure;

        SqlCommand cmd2 = new SqlCommand("Command2", cn);
        cmd2.CommandType = CommandType.StoredProcedure;

        cn.Open();

        // Execute cmd1
        // Execure cmd2

    }

【讨论】:

    【解决方案2】:

    示例;有点伪,但你应该明白这个概念。

    dim cnn as connection 
    dim cmd as command 
    dim cmd2 as command 
    dim str1 as string
    dim str2 as string 
    
    cnn.open
    
    cmd.connection = cnn 
    cmd.command = str1 
    cmd.execute
    
    cmd2.connection = cnn 
    cmd2.command = str2
    cmd2.execute
    
    cnn.close
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多