计算webapi中的记录



我想问一下如何使用webapi中的数据获取总数。我尝试过使用if子句来计算总记录,但它只显示总计数,数据没有以xml格式显示。

有没有If子句?

json格式示例:

{
Count: 2
[
{
"userid": 1,
"name": "emily",
"mobile": "98724954",
"Age": 24,
"Date": 2020-10-04T21:05:00
},
"userid": 2,
"name": "peter",
"mobile": "42896592",
"Age": 35,
"Date": 2021-05-25T11:20:00
}
]

如何使计数像xml格式中的示例一样?

问题:如何在web api中统计数据或响应,并将数据与xml格式一起显示?

我的代码:


[HttpGet]
public IHttpActionResult Get()
{
List<TestClass> draft = new List<TestClass>();
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "SELECT UserID, Name, Mobile, Age, Date From tbluser";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataReader sdr = sqlcomm.ExecuteReader();
while (sdr.Read())
{
draft.Add(new TestClass()
{
UserId = sdr.GetString(0),
Name = sdr.GetString(1),
Mobile = sdr.GetString(2),
Age = sdr.GetInt32(3),
Date = sdr.GetDateTime(4)
});
}
return Ok(draft);
}
if(test.Count>0)
{
return Ok(test.Count());
}
else
{
return Ok("None");
};

结果:

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">10</int>

类别:

public class TestClass
{
public string UserId { get; set; }
public string Name { get; set; }
public string Mobile { get; set; }
public int Age { get; set; }
public DateTime Date { get; set; }
}

更新

[HttpGet]
public IHttpActionResult Get()
{
List<TestClass> draft = new List<TestClass>();
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(mainconn);
string sqlquery = "SELECT UserID, Name, Mobile, Age, Date From tbluser";
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
SqlDataReader sdr = sqlcomm.ExecuteReader();
while (sdr.Read())
{
draft.Add(new TestClass()
{
UserId = sdr.GetString(0),
Name = sdr.GetString(1),
Mobile = sdr.GetString(2),
Age = sdr.GetInt32(3),
Date = sdr.GetDateTime(4)
});
}
var apiResult = new ApiResult
{
Count = api.Length,
Data = api
};
var result= Ok(apiResult);
}

您还没有给出一个输出期望值的示例,因此很难确切地知道XML的外观。还要注意,您的(无效(JSON示例与您的TestClass不匹配,所以我认为您的TestClass是正确的。

首先,添加一个包装器类,以便您也可以将Count序列化为XML:

public class TestApiResult
{
public int Count { get; set; }
public List<TestClass> Data { get; set; }
}

更新您的控制器操作,将您的数据放入此类的实例中:

[HttpGet]
public IHttpActionResult Get()
{
string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
using var sqlconn = new SqlConnection(mainconn);
string sqlquery = "SELECT UserID, Name, Mobile, Age, Date From tbluser";
sqlconn.Open();
using var sqlcomm = new SqlCommand(sqlquery, sqlconn);
using var sdr = sqlcomm.ExecuteReader();
var data = new List<TestClass>();
while (sdr.Read())
{
data.Add(new TestClass()
{
UserId = sdr.GetString(0),
Name = sdr.GetString(1),
Mobile = sdr.GetString(2),
Age = sdr.GetInt32(3),
Date = sdr.GetDateTime(4)
});
}
// The important bit:
var apiResult = new ApiResult
{
Count = data.Count,
Data = data
};
var result= Ok(apiResult);
}

例如,这将导致以下XML:

<?xml version="1.0" encoding="utf-8"?>
<TestApiResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Count>3</Count>
<Data>
<TestClass>
<UserId>fooID</UserId>
<Name>fooName</Name>
<Mobile>fooMobile</Mobile>
<Access>1</Access>
<Date>2022-08-11T00:00:00+00:00</Date>
</TestClass>
<TestClass>
<UserId>barID</UserId>
<Name>barName</Name>
<Mobile>barMobile</Mobile>
<Access>2</Access>
<Date>2022-08-12T00:00:00+00:00</Date>
</TestClass>
<TestClass>
<UserId>bazID</UserId>
<Name>bazName</Name>
<Mobile>bazMobile</Mobile>
<Access>3</Access>
<Date>2022-08-13T00:00:00+00:00</Date>
</TestClass>
</Data>
</TestApiResult>

注意:我在您的代码中添加了一些using语句。记住这一点很重要,这样当您不再使用对象时,应用程序资源就会得到正确的处理。请参阅using的文档以获取更多信息。

检查一下这把小提琴进行测试。

相关内容

  • 没有找到相关文章

最新更新