Firebird FBConnection失去connectionString

时间:2014-05-14 15:29:39

标签: c# asp.net oledb firebird

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

现在我正在初始化并使用连接,如下面的代码示例所示。 我偶尔会得到错误“connectionstring未初始化”。连接对象不为null,但connectionstring似乎为null。 是什么导致这种行为?我应该在每次访问方法时重新初始化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();
                               }
                        }
                    }
        }

1 个答案:

答案 0 :(得分:0)

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