我的任务是从数据库中获取结果,如下所示:
[
{
LicenseNumber = "xxx", //string, that can contain numbers
EMailAddress = "people@gmail.com",
PhoneNumber = "+370 600 00001",
OrganisationName = "work place"
}
]
我收到了以下代码:
public class DataController : ApiController
{
[Authorize]
[HttpGet]
[Route("api/participants")]
public JsonResult<Array> Get(string participantCode)
{
JsonResult<Array> result = null;
string connectionString = "Server=db1.lagoon.lt;Database=LagoonOcean_LT;Integrated Security=True";
string queryString =
"SELECT p.LicenseNumber, p.EMailAddress, p.PhoneNumber, w.OrganisationName" +
"FROM [LagoonOcean_LT].[dbo].[Person] p.inner join LagoonOcean_LT.dbo.PersonMainWorkplaceVW w on w.PersonUUID = p.PersonUUID" +
"WHERE LicenseNumber == participantCode;";
int paramValue = 5;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@LicenseNumber", paramValue);
var participants = new List<object>();
try
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
participants.Add(new[]{new
{
LicenseNumber = reader[0],
EMailAddress = reader[1],
PhoneNumber = reader[2],
OrganisationName = reader[3]
}
});
}
reader.Close();
}
connection.Close();
}
catch (Exception ex)
{
throw ex;
}
result = Json<Array>(participants.ToArray());
}
return result;
}
}
但是我收到了错误:
DoctorInfoWebService.dll中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理 附加信息:'。'。
附近的语法不正确我认为选择数据或连接字符串有问题
答案 0 :(得分:3)
FROM [LagoonOcean_LT].[dbo].[Person] p.inner
你想在这做什么?在p之后删除.
。此外,默认架构是dbo,不需要编写它。
答案 1 :(得分:0)
首先,查询错误。如果要使用前缀,则需要将其声明为名称
SELECT p.Name FROM Students p WHERE p.Id = 1
其次,如果您正在使用ADO.NET,也许最好尝试使用EntityFramework和LINQ表达式,它更加清晰和简单。你可以从这些来源看看: LINQ sample EntityFramework