简体   繁体   English

SQL 如何替换值

[英]SQL How to replace a value

I'm using a Database I was able to get all the data from the database and save it in a List<>.我正在使用数据库,我能够从数据库中获取所有数据并将其保存在 List<> 中。 I made changes in the List<> using a DataGrid and now I want to replace each Database value for the List<> values WHERE the List.ID == Database ID.我使用 DataGrid 在 List<> 中进行了更改,现在我想替换 List<> 值的每个数据库值,其中 List.ID == 数据库 ID。 I use Dapper (In case it matters);我使用 Dapper(以防万一);

public void SetData(List<DatabaseInfoModel> database)
    {
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection("Server=.\\SQLEXPRESS; Database=XXXX; User Id=XXXX; Password=password;"))
        {
            foreach(DatabaseInfoModel item in database)
            {
                connection.Execute($"UPDATE DataTable " +
                                   $"SET Name = {item.Name}, " +
                                   $"    Description = {item.Description}, " +
                                   $"    Record = {item.Record} " +
                                   $"    WHERE ID = {item.ID}");
            }

        }
    }

you can pass a model, ex您可以通过 model,例如

.Execute("update mydogs set age=@Age where id=@Id",dog); .Execute("update mydogs set age=@Age where id=@Id",dog);

"dog" is your model “狗”是你的 model

UPDATE DataTable
SET 
Name = REPLACE(Name, item.Name, 'Replacement Value')
.
.
WHERE ID = {item.ID}

Is this what you are looking for?这是你想要的? In case you are looking for the syntax, here is more information: https://docs.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-ver15如果您正在寻找语法,这里有更多信息: https://docs.microsoft.com/en-us/sql/t-sql/functions/replace-transact-sql?view=sql-server-ver15

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

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