简体   繁体   English

在实体框架中调用存储过程后返回所有行

[英]return all row after call stored procedure in entity framework

The following stored procedure has been created in the database, stored procedure works correctly in database: 在数据库中创建了以下存储过程,该存储过程在数据库中可以正常工作:

create procedure [dbo].[search_pazhoheshgar]
@se_code_melli varchar(10) = NULL,
@se_name nvarchar(30) = NULL,
@se_family nvarchar(30) = NULL,
@se_name_uni nvarchar(100) = NULL,
@se_name_reshte_tahsili nvarchar(50) = NULL
AS
begin try
begin tran
SET NOCOUNT ON;
select sabt.code_melli, sabt.name, sabt.family, sabt_como_univercity.name_uni,
sabt_como_reshte.name_reshte_tahsili
from sabt 
inner join sabt_como_univercity ON sabt.univercity = sabt_como_univercity.id_uni
inner join sabt_como_reshte ON sabt.name_reshte = sabt_como_reshte.id_reshte_tahsili
where 
sabt.code_melli like '%' + @se_code_melli + '%' or 
sabt.name like '%' + @se_name + '%' or  
sabt.family like '%' + @se_family + '%' or
sabt_como_univercity.name_uni like '%' + @se_name_uni + '%' or
sabt_como_reshte.name_reshte_tahsili like '%' + @se_name_reshte_tahsili + '%'
commit tran
end try
begin catch
rollback tran
return -1
end catch

With the commands below, I want to run stored procedure from C# program : 使用以下命令,我想从C#程序运行存储过程:

SqlParameter[] sqlParams;
string sqlQuery;
sqlQuery = "search_pazhoheshgar @se_code_melli, @se_name, @se_family, @se_name_uni, @se_name_reshte_tahsili";

sqlParams = new SqlParameter[]
        {
new SqlParameter { ParameterName = "@se_code_melli",  Value = (object)textBox23.Text ?? DBNull.Value},
new SqlParameter { ParameterName = "@se_name",  Value = (object)textBox22.Text ?? DBNull.Value},
new SqlParameter { ParameterName = "@se_family",  Value = (object)textBox21.Text ?? DBNull.Value},
new SqlParameter { ParameterName = "@se_name_uni",  Value = (object)comboBox11.Text ?? DBNull.Value},
new SqlParameter { ParameterName = "@se_name_reshte_tahsili",  Value = (object)comboBox12.Text ?? DBNull.Value}
        };

using (SamenEntities dbContext = new SamenEntities())
{
    dataGridView1.DataSource = dbContext.Database.SqlQuery<search_pazhoheshgar_Result>(sqlQuery, sqlParams).ToList();
}

But after running all the existing rows are displayed from the database. 但是运行后,所有现有行都从数据库中显示出来。 Also, I used the following Way to run stored procedure. 另外,我使用以下方式来运行存储过程。 But again, all rows are displayed: 但是再次显示所有行:

using (SamenEntities dbContext = new SamenEntities())
    {
    dataGridView1.DataSource = dbContext.search_pazhoheshgar(textBox23.Text, textBox22.Text, textBox21.Text, comboBox11.Text, comboBox12.Text);
    }

How can I fix the searcher problem? 如何解决搜索者问题?

Your query combines a check on the search criteria with an OR condition. 您的查询将对搜索条件的检查与OR条件结合在一起。 That means, when only one of entries an empty string, you will get the entire result set (except when the respective column is NULL ). 这意味着,当只有一个条目为空字符串时,您将获得整个结果集(除非相应的列为NULL )。

Your expression Value = (object)textBox23.Text ?? DBNull.Value 您的表达式Value = (object)textBox23.Text ?? DBNull.Value Value = (object)textBox23.Text ?? DBNull.Value will never yield DBNull , because TextBox.Text will have the value "" instead of null . Value = (object)textBox23.Text ?? DBNull.Value永远不会产生DBNull ,因为TextBox.Text将具有值""而不是null So you will pass an empty string to your stored procedure, which will cause... 因此,您将向您的存储过程传递一个空字符串,这将导致...

abt.code_melli LIKE '%' + @se_code_melli + '%'

...to evaluate to... ...评估...

abt.code_melli LIKE '%%'

Which will yield the entire result set (except where the column contains a NULL value), no matter what is in your other parameters (because they are combined with OR ). 无论您的其他参数是什么(因为它们与OR组合在一起),它将产生整个结果集(除非该列包含NULL值)。

You have to guard yourself against that in your WHERE condition: 你必须保卫自己免受在WHERE条件:

--[...]
WHERE 
    @se_code_melli IS NOT NULL AND @se_code_melli <> '' AND
        sabt.code_melli LIKE '%' + @se_code_melli + '%' OR 
    @se_name IS NOT NULL AND @se_name <> '' AND sabt.name LIKE '%' + @se_name + '%' OR  
    --[...]

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

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