我有一个组合框来设置@NULLFather' and '@NULLMother
的值。勾选组合框时,存储过程的WHERE
子句检查@NULLFather' and '@NULLMother
的值,并根据该值给出结果。但我无法让它发挥作用。这是代码。我也试过了这个案例,但仍然没有成功。
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
答案 0 :(得分:0)
我实施了不同的解决方案:
首先调用存储过程并将其保存到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; }
}