【发布时间】:2017-03-15 20:19:41
【问题描述】:
我的任务是从数据库中获取结果,应该如下所示:
[
{
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”类型的异常,但未在用户代码中处理 附加信息:'.' 附近的语法不正确。
我认为选择数据或连接字符串有问题
【问题讨论】:
-
我认为您的 FROM 语句
FROM [LagoonOcean_LT].[dbo].[Person] p.inner join中有错字。注意 '.'在p之后?尝试删除它
标签: c# mysql json select ado.net