我有一个Web API,它返回一个数据表
public async Task<IHttpActionResult> SelectExtension(DateTime start, DateTime end, string institution)
{
var identity = (ClaimsIdentity)User.Identity;
var facility= claimIdentity.GetIdentityValue("Institution", identity).First();
DataTable table = new DataTable("Result");
var cmd = new SqlCommand();
using (SqlConnection con = connection.DB())
{
try
{
con.Open();
var query = generalData.SelectExtension(start, end, facility, institution);
cmd = command.CreateQuery(query.Query, query.ProcdureParameters, con);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
sqlDataAdapter.Fill(table);
}
catch (Exception e)
{
var error = $"Code: {e.HResult}\n Message: {e.Message}\n InnerException: {e.InnerException}\n Stack: {e.StackTrace}";
await stream.WriteAsync(error);
return InternalServerError();
}
}
return Ok(table);
}
其中有一个数据类型为DateTime的列
table.Rows[6]["ApprovedExtendedEndDate"]
{11/25/2021 12:00:00 AM}
Date: {11/25/2021 12:00:00 AM}
Day: 25
DayOfWeek: Thursday
DayOfYear: 329
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 11
Second: 0
Ticks: 637733952000000000
TimeOfDay: {00:00:00}
Year: 2021
当返回到客户端时,列的数据类型将更改为string
- Http客户端代码 *
public async Task<DataTable> SelectExtension(DateTime start, DateTime end, string institution)
{
try
{
var response = await ClientHttp.HttpClient.Client.GetAsync($"{controller}/SelectExtension/{start:yyyy-MM-dd}/{end:yyyy-MM-dd}/{institution}");
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<DataTable>();
return result;
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
FormAction.Alert("You do not have permission to access this resource", FormAction.enmType.Error);
return null;
}
return null;
}
列中的值现在变为字符串
result.Rows[6]["ApprovedExtendedEndDate"]
"11/25/2021 00:00:00"
任何人都可以请帮助我确定为什么它是无法转换为一个日期时间。
它不是数据表中唯一的日期时间列,但此特定列返回字符串。
1条答案
按热度按时间rkttyhzu1#
每当第一行的DateTime列中包含空值时,我就会看到这种行为,由于某种原因,序列化程序不执行任何操作,而是设置默认数据类型(字符串),而其他行的值则通过调用ToString方法来设置,该方法使用默认的Thread来格式化日期。CurrentCulture