编辑
删除assetItem 上的对象初始化程序似乎已经解决了我的问题,但为什么呢?
结束编辑
我似乎无法想出一种方法来从我的数据库中读取一个 INT 并将其转换为 emun。
我已经尝试了 Cast int to enum in C# 和How to (efficient) convert (cast?) a SqlDataReader field to its对应的c# type中的建议?没有成功。
using (var connection = new SqlConnection(_sqlstring))
{
using (var command = new SqlCommand("EXEC GetAllEncodedMedia", connection))
{
try
{
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
if (reader.HasRows)
{
var assetItem = new MediaServices.EncodedAssets
{
Id = reader.IsDBNull(0) ? 0 : reader.GetInt32(0),
PublishedName = reader.IsDBNull(1) ? null : reader.GetString(1),
PublishUri = reader.IsDBNull(2) ? new Uri("http://www.null.com") : new Uri(reader.GetString(2)),
EncodePreset = reader.IsDBNull(3) ? 0 : (MediaServices.EncodePresetsForSmoothStreaming)reader.GetInt32(3),
AssetId = reader.IsDBNull(4) ? null : reader.GetString(4),
EncoderJobId = reader.IsDBNull(5) ? 0 : reader.GetInt32(5),
//EncoderState = reader.IsDBNull(6) ? 0 : (JobState)reader.GetInt32(6),
//AssetState = reader.IsDBNull(7) ? 0 : (MediaServices.InternalAssetState)reader.GetInt32(7),
GroupId = reader.IsDBNull(8) ? 0 : reader.GetInt32(8),
Published = !reader.IsDBNull(2)
};
listReturn.Add(assetItem);
}
}
}
catch (Exception ex)
{
//error
}
}
}
[DataContract]
public enum EncodePresetsForSmoothStreaming
{
[EnumMember]
[Description("H264 Smooth Streaming 1080p")]
H264SmoothStreaming1080P,
[EnumMember]
[Description("H264 Smooth Streaming 720p")]
H264SmoothStreaming720P,
[EnumMember]
[Description("H264 Smooth Streaming 720p for 3G or 4G")]
H264SmoothStreaming720Pfor3Gor4G,
[EnumMember]
[Description("H264 Smooth Streaming SD 16x9")]
H264SmoothStreamingSd16X9,
[EnumMember]
[Description("H264 Smooth Streaming SD 4x3")]
H264SmoothStreamingSd4X3
}
如何将序数 3 (int) 转换为EncodePresetsForSmoothStreaming
(enum)?