Sql server 带有条件Where子句的Sql Server存储过程
我有一个组合框,用于设置@NULLFather'和@NULLMother的值。勾选组合框后,存储过程的WHERE子句将检查@NULLFather'和'@NULLMother'的值,并根据这些值给出结果。但我不能让它工作。这是代码。我也尝试过这种情况,但仍然没有成功Sql server 带有条件Where子句的Sql Server存储过程,sql-server,tsql,Sql Server,Tsql,我有一个组合框,用于设置@NULLFather'和@NULLMother的值。勾选组合框后,存储过程的WHERE子句将检查@NULLFather'和'@NULLMother'的值,并根据这些值给出结果。但我不能让它工作。这是代码。我也尝试过这种情况,但仍然没有成功 USE myStudentDB GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[ManualSearchStudents] --
USE myStudentDB
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ManualSearchStudents]
-- Add the parameters for the stored procedure here
@NullFather int = 1,
@NullMother int = 1
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT StudentName, FatherName, MotherName
FROM StudentTable
WHERE IF(NULLFather > 0) THEN FatherName IS NULL
AND IF(NullMother > 0) THEN MotherName IS NULL
ORDER BY StudentTable.StudentID DESC
END
我实施了一个不同的解决方案: 首先调用存储过程并将其保存到dataTable。从组合框生成sql语句字符串。将新的sql字符串应用于dataTable。问题解决了。应用这个,效果很好。代码如下:
private DataTable StudentDataExtract()
{
DataTable ds = new DataTable();
try
{
string sqlSelect = "SearchStudents";
EntityConnection scontext = new EntityConnection();
SqlConnection c = new SqlConnection(scontext.Database.Connection.ConnectionString);
SqlCommand cmd = new SqlCommand(sqlSelect, c); //c.con is the connection string
cmd.CommandType = CommandType.StoredProcedure;
c.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
da.Fill(ds);
c.Close();
if (GetFilter != "")
{
ds = ManualFilter(ds);
}
else
{
string dataOrder = "SID DESC";
DataView dv = ds.DefaultView;
dv.Sort = dataOrder;
ds = dv.ToTable();
}
return ds;
}
catch (SqlException e)
{
return ds;
}
}
private DataTable ManualFilter(DataTable OldDataTable)
{
DataTable NewDataTable = new DataTable();
try
{
NewDataTable = OldDataTable.Select(GetFilter, dataOrder).CopyToDataTable();
return NewDataTable;
}
catch (SqlException)
{
return NewDataTable;
}
catch(Exception)
{
return NewDataTable;
}
}
private string getfilter = "";
public string GetFilter
{
get
{
IEnumerable<String> selectedData = combolist.Where(d => d.IsSelected).Select(d => d.myTableValue);
string singleString = string.Join(" OR ", selectedData.ToArray());
return singleString;
}
set
{
getfilter = value; OnPropertyChanged("GetFilter"); OnPropertyChanged("GetStudentData");
}
}
public DataTable GetStudentData
{
get
{
return this.StudentDataExtract();
}
}
public class ComboSet : ViewModelBase
{
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; OnPropertyChanged("IsSelected"); OnPropertyChanged("GetFilter"); }
}
private string mydataproperty;
public string myDataProperty
{
get { return mydataproperty; }
set { mydataproperty = value; }
}
private string mytablevalue;
public string myTableValue
{
get { return mytablevalue; }
set { mytablevalue = value; }
}
}
private ObservableCollection<ComboSet> combolist;
public ObservableCollection<ComboSet> ComboList
{
get { return combolist; }
set { combolist = value; }
}
在@NullFather=0或FatherName为NULL且@NullMother=0或MotherName为NULL的情况下尝试,如果@NullFather的值大于0,则该方法有效。但当值为=0时,则不会显示结果。使用类似于ORM的EF并使用LINQ构造此类查询要容易得多。使用可选参数不会获得任何性能好处,因为SQL Server会根据第一次执行的参数缓存执行计划。我认为您是对的。使用实体框架似乎是一条出路。