[英]how to select data with ado.net from mssql
我的任务是从数据库中获取结果,如下所示:
[
{
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 [LagoonOcean_LT].[dbo].[Person] p.inner
您想在这里做什么? 删除.
p。之后 同样,默认架构为dbo,无需编写。
首先,查询是错误的。 如果要使用前缀,则需要将其声明为名称
SELECT p.Name FROM Students p WHERE p.Id = 1
其次,如果您使用的是ADO.NET,也许最好尝试使用EntityFramework和LINQ表达式来实现,这更加清晰和简单。 您可以从这些来源中查看: LINQ示例 EntityFramework
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.