Web API 查询 SQL 服务器和 return 结果未按预期工作
Web API to query SQL Server and return result is not working as expected
我正在尝试从 Web API 和 return 连接到 SQL 服务器,结果集为 JSON。但是我在这里显示的代码没有按预期工作。我正在尝试 return 整个查询响应作为 JSON:
[HttpGet]
public HttpResponseMessage Getdetails(string ROOM)
{
string commandText = "SELECT * from [TDB].[dbo].[results_vw] where ROOM = @ROOM_Data";
string connStr = ConfigurationManager.ConnectionStrings["TDBConnection"].ConnectionString;
var jsonResult = new StringBuilder();
using (SqlConnection connection = new SqlConnection(connStr))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@ROOM_Data", SqlDbType.VarChar);
command.Parameters["@ROOM_Data"].Value = ROOM;
connection.Open();
var reader = command.ExecuteReader();
if (!reader.HasRows)
{
jsonResult.Append("[]");
}
else
{
while (reader.Read())
{
jsonResult.Append(reader.GetValue(0).ToString());
}
}
var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
response.Content = new StringContent(jsonResult.ToString());
connection.Close();
return response;
}
}
此代码 return 此结果:
333838362692368203368203368203362692368203359544362692368203362692368203362692368203368203
我期望 JSON 为
{"data":
[
{"R_ID":"368203","ROOM":"K2"},
{"R_ID":"368203","ROOM":"K2"}
]}
现在我创建了一个名为 DatabaseResult
的模型 class 来存储响应,但我不确定如何将结果存储到控制器中的模型 class
public class DatabaseResult
{
public int r_id { get; set; }
public string room { get; set; }
}
当前结果是因为您只是return每行第一列的值并将其添加到字符串生成器。
创建模型的新实例并使用来自 reader 的值填充每一行。
[HttpGet]
public IHttpActionResult Getdetails(string ROOM) {
string commandText = "SELECT * from [TDB].[dbo].[results_vw] where ROOM = @ROOM_Data";
string connStr = ConfigurationManager.ConnectionStrings["TDBConnection"].ConnectionString;
var jsonResult = new StringBuilder();
using (SqlConnection connection = new SqlConnection(connStr)) {
using (SqlCommand command = new SqlCommand(commandText, connection)) {
command.Parameters.Add("@ROOM_Data", SqlDbType.VarChar);
command.Parameters["@ROOM_Data"].Value = ROOM;
connection.Open();
List<DatabaseResult> records = new List<DatabaseResult>();
using (var reader = command.ExecuteReader()) {
while (reader.Read()) {
var row = new DatabaseResult {
r_id = (int)reader["r_id"],
room = (string)reader["room"],
//...other properties.
};
records.Add(row);
}
return Ok(records);
}
}
}
}
以上使用列名作为索引器从 reader.
中获取值
我正在尝试从 Web API 和 return 连接到 SQL 服务器,结果集为 JSON。但是我在这里显示的代码没有按预期工作。我正在尝试 return 整个查询响应作为 JSON:
[HttpGet]
public HttpResponseMessage Getdetails(string ROOM)
{
string commandText = "SELECT * from [TDB].[dbo].[results_vw] where ROOM = @ROOM_Data";
string connStr = ConfigurationManager.ConnectionStrings["TDBConnection"].ConnectionString;
var jsonResult = new StringBuilder();
using (SqlConnection connection = new SqlConnection(connStr))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@ROOM_Data", SqlDbType.VarChar);
command.Parameters["@ROOM_Data"].Value = ROOM;
connection.Open();
var reader = command.ExecuteReader();
if (!reader.HasRows)
{
jsonResult.Append("[]");
}
else
{
while (reader.Read())
{
jsonResult.Append(reader.GetValue(0).ToString());
}
}
var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
response.Content = new StringContent(jsonResult.ToString());
connection.Close();
return response;
}
}
此代码 return 此结果:
333838362692368203368203368203362692368203359544362692368203362692368203362692368203368203
我期望 JSON 为
{"data":
[
{"R_ID":"368203","ROOM":"K2"},
{"R_ID":"368203","ROOM":"K2"}
]}
现在我创建了一个名为 DatabaseResult
的模型 class 来存储响应,但我不确定如何将结果存储到控制器中的模型 class
public class DatabaseResult
{
public int r_id { get; set; }
public string room { get; set; }
}
当前结果是因为您只是return每行第一列的值并将其添加到字符串生成器。
创建模型的新实例并使用来自 reader 的值填充每一行。
[HttpGet]
public IHttpActionResult Getdetails(string ROOM) {
string commandText = "SELECT * from [TDB].[dbo].[results_vw] where ROOM = @ROOM_Data";
string connStr = ConfigurationManager.ConnectionStrings["TDBConnection"].ConnectionString;
var jsonResult = new StringBuilder();
using (SqlConnection connection = new SqlConnection(connStr)) {
using (SqlCommand command = new SqlCommand(commandText, connection)) {
command.Parameters.Add("@ROOM_Data", SqlDbType.VarChar);
command.Parameters["@ROOM_Data"].Value = ROOM;
connection.Open();
List<DatabaseResult> records = new List<DatabaseResult>();
using (var reader = command.ExecuteReader()) {
while (reader.Read()) {
var row = new DatabaseResult {
r_id = (int)reader["r_id"],
room = (string)reader["room"],
//...other properties.
};
records.Add(row);
}
return Ok(records);
}
}
}
}
以上使用列名作为索引器从 reader.
中获取值