title: .net core insert using dapper with parameters
body: <p>I'm new to using Dapper and I'm a bit lost on following the instructions for an insert statement.</p>
  
  <p>I have the following model:</p>
  
  <p><strong>User</strong></p>
  
  <pre><code>public class User
  {
      public int UserId { get; set;}
      public string UserName { get; set; }
      public string Password { get; set; }
  }
  </code></pre>
  
  <p>And the following controller which is retrieving data from a JSON ajax call (I have tried 3 different ways based on three different answers I've seen.</p>
  
  <p><strong>Attempt 1</strong> <em>Error: Incorrect syntax near UserId</em></p>
  
  <pre><code>public IActionResult UserData([FromBody] User user)
  {
      using (SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS..."))
      {
          cn.Query&lt;User&gt;(@"INSERT INTO User VALUES UserId = @UserId, UserName = @UserName, Password = @Password", 
          new
          {
              UserId = user.UserId,
              UserName = user.UserName,
              Password = user.Password
           });
  
       return StatusCode(200);
       }
  }
  </code></pre>
  
  <p><strong>Attempt 2</strong> <em>Error: Incorrect syntax near the keyword Table</em></p>
  
  <pre><code>public IActionResult UserData([FromBody] User user)
  {
      using (SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS..."))
      {
          cn.Execute(@"INSERT Table(User) VALUES(@UserId, @UserName, @Password)", 
          new
          {
              UserId = user.UserId,
              UserName = user.UserName,
              Password = user.Password
           });
  
       return StatusCode(200);
       }
  }
  </code></pre>
  
  <p><strong>Attempt 3 (Tried using Dapper.Contrib)</strong> <em>Error: Invalid object name Users (I don't know what this is, Users doesn't exist anywhere as I don't use plurals</em></p>
  
  <pre><code>public IActionResult UserData([FromBody] User user)
  {
      using (SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS..."))
      {
          cn.Insert&lt;User&gt;(new User
          {
              UserId = user.UserId,
              UserName = user.UserName,
              Password = user.Password
           });
  
       return StatusCode(200);
       }
  }
  </code></pre>