是否可以在没有Entity框架的情况下使用Web API并从Web API控制器调用存储过程?
答案 0 :(得分:4)
这是可能的,如下例所示:
public async Task<IHttpActionResult> Get()
{
DataTable dt = new DataTable();
using (SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
await sqlConnection.OpenAsync();
using (SqlCommand cmd = new SqlCommand("sp_GetData", sqlConnection))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
{
dt.Load(reader);
}
}
}
if ((dt == null) || (dt.Rows.Count == 0))
{
return NotFound();
}
return Ok(dt);
}
希望它有所帮助!