简体   繁体   English

更新表 SQL 查询:SqlConnection

[英]Update Table SQL Query: SqlConnection

I would like to update a record based on Id parameter, I have tried following step but that does not seem correct way to do because it generates compilation error:我想根据 Id 参数更新记录,我尝试了以下步骤,但这似乎不是正确的方法,因为它会产生编译错误:

public async Task CustomerUpdateAsync(string customerId)
{
  await using var sqlConnection = new SqlConnection(_connectionString);
            {
                var sqlQuery = "UPDATE Customer(CustomerId,Name,Address,PostalCode,City)" +
                          "SET (@CustomerId,@Name,@Address,@PostalCode,@City)" +
                          $"WHERE CustomerId=@CustomerId", new {CustomerId = customerId};

                await sqlConnection.ExecuteAsync(sqlQuery, customerId);
            }
}

Error:错误:

only assignment call increment decrement await and new object expressions can be used as a statement只有assignment call increment decrement await和new object表达式可以作为语句

You're after something like:你追求的是:

await sqlConnection.ExecuteAsync(@"
UPDATE Customer
SET    Name = @name, Address = @address,
       PostalCode = @postalCode, City = @city
WHERE  CustomerId=@customerId",
     new { customerId, name, address, postalCode, city });

However, I don't know where you intend to get name , address , etc from - they aren't shown in the question.但是,我不知道您打算从哪里获得nameaddress等 - 它们没有显示在问题中。

Thanks guys I got it resolved by tweaking little:-谢谢大家,我通过稍微调整解决了它:-

 public async Task CustomerUpdateAsync(Customer customer)
            {
                await using var sqlConnection = new SqlConnection(_connectionString);
                {
                    var sql = "UPDATE Customer SET Name=@Name,Address=@Address, PostalCode=@PostalCode," +
                              "City=@City WHERE CustomerId=@CustomerId";
                    await sqlConnection.ExecuteAsync(sql, new
                    {
                       CustomerId = customer.CustomerId,
                       Name = customer.Name,
                       Address = customer.Address,
                       PostalCode = customer.PostalCode,
                       City = customer.City
                    });
                }

I am giving an example of single value update operation based on Id filed.我给出了一个基于 Id 字段的单值更新操作的示例。 Your may try as your own.你可以自己尝试。 I used SqlDataAdapter and SqlCommand class for this operation.我使用SqlDataAdapterSqlCommand class 进行此操作。

try
{
    using (SqlConnection con = new SqlConnection(_connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        SqlCommand sc = new SqlCommand("UPDATE [dbo].[TableName] SET [ColumnName] = @ValueToBeUpdated WHERE [Id] = @IdField", con);
        sc.Parameters.AddWithValue("@ValueToBeUpdated", valueToBeUpdated);
        adapter.UpdateCommand = sc;
        con.Open();
        adapter.UpdateCommand.ExecuteNonQuery();
    }
}
catch (Exception ex)
{
    logger.LogError($"Update failed! {ex.Message}");
    throw ex;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM