不能将“cxnDb”作为引用或 out 参数传递,因为它是“using 变量”

本文关键字:因为 using 参数传递 变量 cxnDb 不能 引用 out | 更新日期: 2023-09-27 18:33:24

嗨,我将代码从 VB.NET 转换为 C#。我使用Developer Fusion进行了转换。我最初有很多错误,除了这个,我清除了所有这些错误。这是 VB.net 代码:

   If cxnDb.State = ConnectionState.Open Then
   If AppSettings("skipCredits") Is Nothing Then
   Console.WriteLine("CREDITS")
   chargeCredits(cxnDb)
   Console.WriteLine()
   End If 

这是我拥有的等效 C# 代码:

     if (cxnDb.State == ConnectionState.Open)
      {
     if (System.Configuration.ConfigurationManager.AppSettings["skipCredits"] == null)
      {
      Console.WriteLine("CREDITS");
      chargeCredits( ref cxnDb);
      Console.WriteLine();
      }

但是当我尝试构建我的解决方案时,我得到:

无法将"cxnDb"作为引用或 out 参数传递,因为它是"使用变量"错误。

如果我删除chargeCredits(cxnDb);的 ref 关键字,则必须使用 ref 关键字传递它。

有人可以帮助我吗?

更新:我的 C# chargeCredits 方法实现如下:

     private static void chargeCredits( SqlConnection cxnFinanceDb)
    {
        using (SqlCommand cmdCredits = cxnDb.CreateCommand())
        {
            cmdCredits.CommandType = CommandType.StoredProcedure;
            cmdCredits.CommandText = "bill_Credits";
            cmdCredits.CommandTimeout = 60;
            try
            {
                cmdCredits.ExecuteNonQuery();
            }
            catch (SqlException exDetail)
            {
                Console.WriteLine(" + cmdCredits.SqlException :: " + exDetail.Message);
            }
            catch (Exception exDetail)
            {
                Console.WriteLine(" + cmdCredits.Exception :: " + exDetail.Message);
            }
            finally
            {
                cmdCredits.Dispose();
            }
        }
         }

在这里,我删除了我的方法中的 ref 以使其工作。

不能将“cxnDb”作为引用或 out 参数传递,因为它是“using 变量”

这里不需要ref。我不确定你从哪里得到的,但在你的情况下没有必要。

因此,最简单的解决方案是将其删除。

这意味着从此处删除它:

chargeCredits( ref cxnDb);

以及您的chargeCredits方法。我假设它是这样的:

public void chargeCredits(ref SqlConnection cxnDB)

也从那里删除它。