Firebird FBConnection losing connectionString
本文关键字:connectionString losing FBConnection Firebird | 更新日期: 2023-09-27 18:03:16
使用FBConnection给我带来了一些麻烦。在Firebird Ole-Db的示例中,我只找到使用静态main方法的示例,但我不确定如何在实例方法中实现FbConnection的使用。
现在我正在初始化和使用连接,如下面的代码示例所示。每隔一段时间,我得到错误"connectionstring未初始化"。连接对象不为空,但connectionstring似乎为空。是什么导致了这种行为?我是否应该在每次访问该方法时重新初始化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();
}
}
}
}
Mark Rotteveel的评论是正确的。显然,using子句意味着资源在块的末尾被处理掉。这意味着每次使用"using"块时,我都需要创建一个新的连接。