C# EF SqlQuery始终将具有空值的参数传递给具有多个参数要求的存储过程
我在使用EFC# EF SqlQuery始终将具有空值的参数传递给具有多个参数要求的存储过程,c#,sql-server,asp.net-mvc,entity-framework,stored-procedures,C#,Sql Server,Asp.net Mvc,Entity Framework,Stored Procedures,我在使用EFSqlQuery和存储过程及其多个必需参数时遇到问题。我到处都找过了,我有编写代码的能力,因为如果我使用内联查询。它起作用了 这是密码 string callSP = "ListDirectoryMember @Country,@Lastname,@State,@Phonecd,@City,@Spec,@Zip,@Language"; var Member = await _db.Database.SqlQuery<MemberInfo>(callSP,
SqlQuery
和存储过程及其多个必需参数时遇到问题。我到处都找过了,我有编写代码的能力,因为如果我使用内联查询。它起作用了
这是密码
string callSP = "ListDirectoryMember @Country,@Lastname,@State,@Phonecd,@City,@Spec,@Zip,@Language";
var Member = await _db.Database.SqlQuery<MemberInfo>(callSP,
//new object[]
//{
new SqlParameter("Lastname", string.IsNullOrEmpty(lname) ? (object)DBNull.Value : lname + "%"),
new SqlParameter("Phonecd",string.IsNullOrEmpty(areacode) ? (object)DBNull.Value : "(" + areacode + ")%"),
new SqlParameter("State", string.IsNullOrEmpty(statecd) ? (object)DBNull.Value : statecd ),
new SqlParameter("City",string.IsNullOrEmpty(city) ? (object)DBNull.Value : city),
new SqlParameter("Zip",string.IsNullOrEmpty(zip) ? (object)DBNull.Value : zip),
new SqlParameter("country",string.IsNullOrEmpty(country) ? (object)DBNull.Value : country),
new SqlParameter("Language",string.IsNullOrEmpty(language) ? (object)DBNull.Value : language),
new SqlParameter("Spec",string.IsNullOrEmpty(spec) ? (object)DBNull.Value : spec)
//}
).ToListAsync();
这与存储过程中的查询相同
提前非常感谢这里可能有一些逻辑错误。以这一行为例:
new SqlParameter("@Phonecd",string.IsNullOrEmpty(areacode) ? (object)DBNull.Value : statecd),
您正在检查areacode
是否有值,如果有,则将参数设置为statecd
,这似乎不正确。或者这个:
new SqlParameter("@State", string.IsNullOrEmpty(statecd) ? (object)DBNull.Value : "(" + areacode + ")%"),
您正在检查
statecd
是否有值,然后将参数设置为使用areacode
组成的字符串。你可能只是在你不期望的地方得到了空值,因为你在错误的地方测试了错误的东西。在使用不同的语法进行测试后,我能够让它工作。但是看起来不太好。这里可以进行sql注入吗
string callSP = "ListDirectoryMember @Country = {0},@Lastname = {1},@State = {2},@Phonecd = {3},@City = {4},@Spec = {5},@Zip = {6},@Language = {7}";
var Member = await _db.Database.SqlQuery<MemberInfo>(callSP,
string.IsNullOrEmpty(country) ? (object)DBNull.Value : country,
string.IsNullOrEmpty(lname) ? (object)DBNull.Value : lname + "%",
string.IsNullOrEmpty(statecd) ? (object)DBNull.Value : statecd,
string.IsNullOrEmpty(areacode) ? (object)DBNull.Value : "(" + areacode + ")%",
string.IsNullOrEmpty(city) ? (object)DBNull.Value : city,
string.IsNullOrEmpty(spec) ? (object)DBNull.Value : spec,
string.IsNullOrEmpty(zip) ? (object)DBNull.Value : zip,
string.IsNullOrEmpty(language) ? (object)DBNull.Value : language
).ToListAsync();
string callSP=“ListDirectoryMember@Country={0},@Lastname={1},@State={2},@Phonecd={3},@City={4},@Spec={5},@Zip={6},@Language={7}”;
var Member=await_db.Database.SqlQuery(callSP,
string.IsNullOrEmpty(国家)?(对象)DBNull.Value:country,
string.IsNullOrEmpty(lname)?(对象)DBNull.Value:lname+“%”,
string.IsNullOrEmpty(statecd)?(对象)DBNull.Value:statecd,
string.IsNullOrEmpty(areacode)?(对象)DBNull.Value:(“+areacode+”)%,
string.IsNullOrEmpty(城市)?(对象)DBNull.Value:city,
string.IsNullOrEmpty(spec)?(对象)DBNull.Value:spec,
string.IsNullOrEmpty(zip)?(对象)DBNull.Value:zip,
string.IsNullOrEmpty(语言)?(对象)DBNull.Value:language
).ToListAsync();
非常感谢您查看我的问题。哦,是的..我忘了编辑我的问题..我已经看到了..但不幸的是..它仍然是一样的..很好的理解如果在初始化不同的
SqlParameters()时在参数名之前添加@
,会发生什么
?实际上,原来的问题是@
,因为我尝试了使用和不使用works和inline。您是否尝试将字符串作为:“执行官……”。。。。。并使用以下命令调用:_db.Database.ExecuteSqlCommand?你可以用来调试的另一个技巧是启用你的SQL分析器,这样你就可以看到由EF触发的查询。是的,先生。我也试过了。我可以运行它。检查下面的答案。但我对它不满意。我知道有一种更好更整洁的解决方案。
string callSP = "ListDirectoryMember @Country = {0},@Lastname = {1},@State = {2},@Phonecd = {3},@City = {4},@Spec = {5},@Zip = {6},@Language = {7}";
var Member = await _db.Database.SqlQuery<MemberInfo>(callSP,
string.IsNullOrEmpty(country) ? (object)DBNull.Value : country,
string.IsNullOrEmpty(lname) ? (object)DBNull.Value : lname + "%",
string.IsNullOrEmpty(statecd) ? (object)DBNull.Value : statecd,
string.IsNullOrEmpty(areacode) ? (object)DBNull.Value : "(" + areacode + ")%",
string.IsNullOrEmpty(city) ? (object)DBNull.Value : city,
string.IsNullOrEmpty(spec) ? (object)DBNull.Value : spec,
string.IsNullOrEmpty(zip) ? (object)DBNull.Value : zip,
string.IsNullOrEmpty(language) ? (object)DBNull.Value : language
).ToListAsync();