Ан ASP.NET web restore API с SQL server без использования entity framework?
How to create an ASP.NET Web Restore API with SQL Server without using entity framework?
I'm having trouble finding a tutorial to create an API Rest ASP.NET Web with SQL Server without entity framework, can someone help me? =(
Что я уже пробовал:
соединение:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; using Teste.Controllers; namespace Teste.Models { public class Conexaocs : IDisposable { private SqlConnection connection; public void ConectaBD() { string stringConexao = "Data Source=localhost;Initial Catalog=Teste;Integrated Security=true"; connection = new SqlConnection(stringConexao); connection.Open(); } public void Dispose() { //connection.Close(); } public void InserirMarca(Marca marca) { } public List<Marca> consultaMarcas() { List<Marca> marcas = new List<Marca>(); SqlCommand query = new SqlCommand(); query.Connection = connection; query.CommandText = "@SELECT * FROM Marca"; SqlDataReader leitura = query.ExecuteReader(); while (leitura.Read()) { Marca marca = new Marca(); marca.MarcaId = (int)leitura["MarcaId"]; marca.Nome = (string)leitura["Nome"]; marcas.Add(marca); } return marcas; } } }
и контроллер
sing System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using Teste.Models; namespace Teste.Controllers { public class MarcaController : ApiController { Conexaocs conexao = new Conexaocs(); private static List<Marca> marcas = new List<Marca>(); public List<Marca> Get() { try { using (conexao = new Conexaocs()) { marcas = conexao.consultaMarcas(); return marcas; } } catch (SqlException e) { return marcas; } } }
и ошибка:
{ "Message": "Ocorreu um erro.", "ExceptionMessage": "ExecuteReader: propriedade Connection não foi inicializada.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " em System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)\r\n em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)\r\n em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n em System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)\r\n em System.Data.SqlClient.SqlCommand.ExecuteReader()\r\n em Teste.Models.Conexaocs.consultaMarcas() na D:\\C#\\Teste\\Teste\\Models\\Conexaocs.cs:linha 35\r\n em Teste.Controllers.MarcaController.Get() na D:\\C#\\Teste\\Teste\\Controllers\\MarcaController.cs:linha 25\r\n em lambda_method(Closure , Object , Object[] )\r\n em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters)\r\n em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada ---\r\n em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n em System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada ---\r\n em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n em System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada ---\r\n em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n em System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()" }