public void SaveDapper(UserSql userSql)
 {
     new Microsoft.Data.SqlClient.SqlConnection(_connAzureSql)
     .Insert(new UserSql {
         Name = userSql.Name
     });
 }
Ejemplo n.º 2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "cadastro")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];



            if (req.Method == HttpMethods.Post)
            {
                string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                UserSql data        = System.Text.Json.JsonSerializer.Deserialize <UserSql>(requestBody,
                                                                                            new System.Text.Json.JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = true
                });

                _azureSqlRepository.SaveDapper(data);
                return(new OkObjectResult("Saved"));
            }
            else if (req.Method == HttpMethods.Get)
            {
                var itens = _azureSqlRepository.GetAll();

                return(new OkObjectResult(itens));
            }
            else
            {
                return(new NotFoundResult());
            }
        }
        public void SaveADO(UserSql userSql)
        {
            using (var command = _sqlConnection.CreateCommand())
            {
                command.Connection  = new SqlConnection(_connAzureSql);
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "INSERT INTO UserSqls (Name) values ('@Name')";

                var parameterName = command.CreateParameter();
                parameterName.ParameterName = nameof(userSql.Name);
                parameterName.Value         = userSql.Name;
                command.Parameters.Add(parameterName);

                if (command.Connection.State != System.Data.ConnectionState.Open)
                {
                    command.Connection.Open();
                }

                command.ExecuteNonQuery();

                command.Connection.Close();
            }
        }