0
我是使用Dapper的新手,並且在遵循插入語句的說明時有點遺憾。.net核心插入使用縮略參數
我有以下型號:
用戶
public class User
{
public int UserId { get; set;}
public string UserName { get; set; }
public string Password { get; set; }
}
而下面的控制器,它是由JSON Ajax調用檢索數據基於三個不同的答案(我已經嘗試了3點不同的方法,我
嘗試1錯誤:UserId附近的語法不正確
public IActionResult UserData([FromBody] User user)
{
using (SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS..."))
{
cn.Query<User>(@"INSERT INTO User VALUES UserId = @UserId, UserName = @UserName, Password = @Password",
new
{
UserId = user.UserId,
UserName = user.UserName,
Password = user.Password
});
return StatusCode(200);
}
}
嘗試2錯誤:關鍵字表附近的語法不正確
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);
}
}
嘗試3(嘗試使用Dapper.Contrib)錯誤:無效的對象名稱的用戶(我不噸知道這是什麼,用戶不存在任何地方,因爲我不使用複數
public IActionResult UserData([FromBody] User user)
{
using (SqlConnection cn = new SqlConnection(@"Server=.\SQLEXPRESS..."))
{
cn.Insert<User>(new User
{
UserId = user.UserId,
UserName = user.UserName,
Password = user.Password
});
return StatusCode(200);
}
}