如何使用来自 mssql 的 ado.net select 数据
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;
}
}
但我得到一个错误:
'System.Data.SqlClient.SqlException' 类型的异常发生在 DoctorInfoWebService.dll 但未在用户代码中处理
附加信息:“.”附近的语法不正确。
我认为选择数据或连接字符串有问题
FROM [LagoonOcean_LT].[dbo].[Person] p.inner
你想在这里做什么?去掉p后面的.
。另外默认模式是dbo,不需要写。
第一,查询错误。如果要使用前缀,则需要将其声明为 name
SELECT p.Name FROM Students p WHERE p.Id = 1
其次,如果您使用的是ADO.NET,也许最好尝试使用EntityFramework和LINQ表达式来实现,这样会更加清晰和简单。您可以从这些来源中查看:
LINQ sample
EntityFramework
我的任务是从数据库中获取应该如下所示的结果:
[
{
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;
}
}
但我得到一个错误:
'System.Data.SqlClient.SqlException' 类型的异常发生在 DoctorInfoWebService.dll 但未在用户代码中处理 附加信息:“.”附近的语法不正确。
我认为选择数据或连接字符串有问题
FROM [LagoonOcean_LT].[dbo].[Person] p.inner
你想在这里做什么?去掉p后面的.
。另外默认模式是dbo,不需要写。
第一,查询错误。如果要使用前缀,则需要将其声明为 name
SELECT p.Name FROM Students p WHERE p.Id = 1
其次,如果您使用的是ADO.NET,也许最好尝试使用EntityFramework和LINQ表达式来实现,这样会更加清晰和简单。您可以从这些来源中查看: LINQ sample EntityFramework