所以我在我的 DL 中的 CRUD 方法中有以下内容,它带回了一个照片 blob:
public static MemberPhoto RetrievePhoto(string customerID)
{
Database db = DatabaseFactory.CreateDatabase(Config.DbConnectionString);
using (DbCommand cmd = db.GetSqlStringCommand(string.Format(@" SELECT customerID,
p.Photo as PhotoBlob
FROM
SomeTable t
WHERE
t.PhotoID = (SELECT MAX(t.PhotoID) FROM SomeTable t
WHERE t.customerID = @{0}
GROUP BY t.CustomerID)", "customerID")))
{
try
{
db.AddInParameter(cmd, "@customerID", DbType.String, customerID);
using (IDataReader reader = db.ExecuteReader(cmd))
{
if (reader.Read())
{
Photo photo = new Photo();
photo.LoadFromDataReader(reader);
return photo;
}
}
}
所以它比我直接从管理工作室运行慢大约 10 秒:
SELECT customerID,
t.Photo as PhotoBlob
FROM
SomeTable t
WHERE
t.PhotoID = (SELECT MAX(t.PhotoID) FROM SomeTable t
WHERE t.customerID = '0000900595555'
GROUP BY t.CustomerID)
我不知道是不是因为读者需要一段时间才能阅读 blob 并将其带回来还是什么?如果这是问题,你如何加快速度?它在数据库的瞬间,我运行该查询,这是一个毫秒的结果计时。
更新:
更多信息; 表中的字段类型是该 BLOB 的 SQL Sever 中的 Image 类型。
我也运行了我的单元测试,同样的时间,即使它击中了我的 LoadFromReader:
[Test]
public void GetMemberPhoto_ByContractNumber_ReturnsAValidMemberPhoto()
{
// Arrange
const string customerID = "0044664176";
// Act
DTO.MemberPhoto memberPhoto = MemberPhotoCRUD.RetrieveMemberPhotoFromBlob(customerID);
//Assert
Assert.IsNotNull(memberPhoto);
Assert.IsTrue(memberPhoto.CustomerID > 0);
Assert.IsNotNull(memberPhoto.PhotoBinary.Length > 0);
}