我如何参数化我的sql语句,这样我就可以得到它同时进行过滤和分组原因我现在可以让它进行分组和过滤,但不是同时SelectedParam
值为空时我试图过滤我的群组
这是我在尝试过滤我的群组时遇到的错误
抛出异常:System.Data.dll中的'System.Data.SqlClient.SqlException'附加信息:参数化查询'(@ param1 nvarchar(4000))选择ps.MærketASMærke,P.DataID,PB'期望参数'@param1'
CS
public partial class MainWindow : Window, INotifyPropertyChanged
{
public SqlConnection conn;
public SqlCommand cmd;
string connStrings = ConfigurationManager.AppSettings["Sql"];
string Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.ET,P.Bolter, P.Dæk, P.Fælge ,PS.Krydsmålene from Data.Hjuldata P inner join Data.Mærke PS on P.MærkeID = PS.MærkeID ORDER BY ps.Mærket";
public event PropertyChangedEventHandler PropertyChanged;
private string _selectedParam;
public MainWindow()
{
InitializeComponent();
BindData()
ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);
dataView.GroupDescriptions.Add(new PropertyGroupDescription("Mærke"));
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string SelectedParam { get { return _selectedParam; } set { _selectedParam = value; NotifyPropertyChanged("SelectedParam"); if (_selectedParam == "ingen") { BindData(); } else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; } } }
private void BindData()
{
hjuldata.ItemsSource = kategori().Tables[0].DefaultView;
}
public DataSet kategori()
{
//SQL statement to fetch entries from Hjuldata
DataSet dsdata = new DataSet();
//Open SQL Connection
using (conn = new SqlConnection(connStrings))
{
conn.Open();
//Initialize command object
using (SqlCommand cmd = new SqlCommand(Data, conn))
{
SqlDataAdapter adapters = new SqlDataAdapter(cmd);
//Fill the result set
adapters.Fill(dsdata);
conn.Close();
}
}
return dsdata;
}
public DataSet FilterKategori()
{
string Data = @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model,
P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.ET,P.Bolter,
P.Dæk, P.Fælge ,PS.Krydsmålene from Data.Hjuldata P
inner join Data.Mærke PS on P.MærkeID = PS.MærkeID
WHERE Krydsmål = @param1";
//SQL statement to fetch entries from products
DataSet dsdata = new DataSet();
//Open SQL Connection
using (conn = new SqlConnection(connStrings))
{
conn.Open();
//Initialize command object
using (SqlCommand cmds = new SqlCommand(Data, conn))
{
cmds.Parameters.AddWithValue("@param1", SelectedParam);
SqlDataAdapter adapters = new SqlDataAdapter(cmds);
//Fill the result set
adapters.Fill(dsdata);
conn.Close();
}
}
return dsdata;
}
用于过滤的组合框
<ComboBox x:Name="Krydsmålbox" Foreground="#FF00FB0B" Background="#FF303030"
FontSize="12" Style="{StaticResource ComboBoxTest2}" ItemTemplate="{StaticResource cmbTemplate2}" Margin="7,5,7,1" Grid.Column="4" Grid.Row="1" ItemsSource="{Binding}" SelectedValuePath="Krydsmålene" SelectedValue = "{Binding SelectedParam, ElementName=win, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>
列表视图
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="False" BorderBrush="#FFEAEAEA" BorderThickness="0,0,0,1" >
<Expander.Header>
<StackPanel Orientation="Horizontal" DataContext="{Binding Items}">
<Image Source="{Binding Billed}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center" Margin="0,0,15,0"/>
<TextBlock Text="{Binding Mærke}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding Krydsmålet}" FontWeight="Bold" Foreground="#FFFBFB00" FontSize="22" VerticalAlignment="Bottom" Margin="0,0,150,0" TextAlignment="Center" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:1)
可能使用可选参数创建存储过程,如下所示:
CREATE PROCEDURE usp_getData
@param1 NVARCHAR(1000) = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql Nvarchar(MAX);
SET @Sql = N'Select ps.Mærket AS Mærke
, P.DataID
, P.Billed
, P.Model
, P.Årgang
, P.[Motor Type]
, P.Krydsmål
, P.Centerhul
, P.ET
,P.Bolter
,P.Dæk
, P.Fælge
,PS.Krydsmålene
from Data.Hjuldata P
inner join Data.Mærke PS on P.MærkeID = PS.MærkeID
WHERE 1 = 1 '
+ CASE WHEN @param1 IS NOT NULL
THEN N' AND Krydsmål = @param1 ' ELSE N'' END
Exec sp_executesql @Sql
,N'@param1 NVARCHAR(1000)'
,@param1
END
如果仅传递@Param1
的值,则应用过滤器,否则执行查询而不使用where子句,@param1
参数也是可选的,默认值为null
。
答案 1 :(得分:0)
你可以使用两个sql。如果param1不为null,请使用where where condition。如果为null,请使用没有where条件子句的其他sql。
答案 2 :(得分:0)
发现问题在
public string SelectedParam { get { return _selectedParam; }
set { _selectedParam = value; NotifyPropertyChanged("SelectedParam");
if (_selectedParam == "ingen") { BindData(); }
else { hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView; ; } } }
private void BindData() { hjuldata.ItemsSource = kategori().Tables[0].DefaultView; };`
如果我换了hjuldata.ItemsSource = kategori().Tables[0].DefaultView
使用hjuldata.ItemsSource = FilterKategori().Tables[0].DefaultView;
然后就可以了
但分组仍然没有