【问题标题】:Firebird FBConnection losing connectionStringFirebird FBConnection 失去连接字符串
【发布时间】:2014-05-14 15:29:38
【问题描述】:

FBConnection 的使用给我带来了一些麻烦。 在 Firebird Ole-Db 的示例中,我只找到使用静态 main 方法的示例,但我不确定如何在实例方法中实现 FbConnection 的使用。

现在我正在初始化和使用连接,如下面的代码示例所示。 我时不时地收到错误“连接字符串未初始化”。连接对象不为空,但连接字符串似乎为空。 是什么导致了这种行为?我是否应该在每次访问该方法时重新初始化 FbConnect 对象(使其成为局部变量),或者这在性能方面是一个非常糟糕的主意?

 public class MyUserStore<TUser> : IUserPasswordStore<TUser, int>, IUserStore<TUser, int>, IDisposable where TUser : ApplicationUser, new()
                    { 
                private FbConnection Connection =  new FbConnection("User=-----;" +
                                                 "Password=-------;" +
                                                  "Database=C:\\------\\Testing.GDB;" +
                                                  "DataSource=localhost;" +
                                                  "Dialect=3;Charset=NONE;");
             public Task<TUser> FindByIdAsync(int userId)
                    {
                        if (userId == 0)
                        {
                            throw new ArgumentNullException("userId");
                        }
                        TUser User = null;

                        if (Connection  != null)
                        {
                            FbTransaction transaction = null;     
                            FbDataReader Reader = null;
                            using (Connection)
                            {
                                try
                                {
                                    Connection.Open();

                                    FbCommand Command = new FbCommand(GetByIdQuery, Connection);  
                                    Command.Parameters.AddWithValue("id", userId);
                                    Reader = Command.ExecuteReader();  
                                catch (Exception e)
                                {
                                   if (transaction != null)
                                   {
                                    transaction.Rollback();
                                   }
                                   System.Diagnostics.Debug.WriteLine(e.StackTrace);
                                   return Task.FromResult<TUser>(null);
                               }
                               finally
                               {
                                if (Reader != null)
                                {
                                    Reader.Close();
                                }
                                Connection.Close();
                               }
                        }
                    }
        }

【问题讨论】:

  • 如果您打算在using()-block 中使用连接,我认为您不应该将连接存储在实例字段中。在using 本身中创建连接。

标签: c# asp.net oledb firebird


【解决方案1】:

Mark Rotteveel 的评论是正确的。 显然, using 子句意味着资源正在块的末尾被处理。这意味着我每次使用“使用”块时都需要创建一个新连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2017-11-24
    • 2016-04-15
    • 2012-10-20
    • 2019-12-25
    • 2018-04-22
    • 1970-01-01
    相关资源
    最近更新 更多