简体   繁体   English

ExecuteSqlInterpolated/ExecuteSqlRaw 参数在 asp.net 内核中错误地传递到数据库

[英]ExecuteSqlInterpolated/ExecuteSqlRaw parameters pass to database incorrectly in asp.net core

I am attempting to use ExecuteSqlInterpolated to update my database.我正在尝试使用 ExecuteSqlInterpolated 来更新我的数据库。 However, it seems that there is a problem with my SQL parameters.但是,我的SQL参数好像有问题。 Running the following code correctly updates intField1 and intField2, however stringField becomes the string "@p0".运行以下代码会正确更新 intField1 和 intField2,但 stringField 会变为字符串“@p0”。

_context.Database.ExecuteSqlInterpolated($"UPDATE table SET stringField='{someString}', intField1={someInt}, intField2={someOtherInt} WHERE id='{id}'");

I have already verified that my variables contain the desired values when the string is passed to the method.当字符串传递给方法时,我已经验证了我的变量是否包含所需的值。 I understand that @p0 is what SQL uses to represent the first parameter in the query, but why isn't it being replaced by the string I gave it?我知道@p0 是 SQL 用来表示查询中的第一个参数的,但是为什么它没有被我给它的字符串替换呢? I have also tried using ExecuteSqlRaw but ran into the same issue.我也尝试过使用 ExecuteSqlRaw 但遇到了同样的问题。 My knowledge of SQL is limited at best, I know just enough to get by in web dev, so I'm guessing I'm committing some simple error in crafting the query here, but obviously I'm not sure.我对 SQL 的了解充其量是有限的,我知道在 web 开发人员中勉强过得去,所以我猜我在这里制作查询时犯了一些简单的错误,但显然我不确定。

I know it's late but just don't use quotation mark with your parameters, specially where you use int data-type (delete single quotes)我知道已经很晚了,但不要在参数中使用引号,特别是在使用 int 数据类型的地方(删除单引号)

_context.Database.ExecuteSqlInterpolated($"UPDATE table SET stringField={someString}, intField1={someInt}, intField2={someOtherInt} WHERE id={id}");

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

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