我如何参数化我的sql语句,以便我可以将它同时做到过滤和分组原因,现在我可以将它分组和过滤,但不能在同一时间SelectedParam
值为空,当我试图筛选我组如何参数化sql语句用于分组和过滤
这个时候我想我的过滤组
异常抛出我得到的错误:在System.Data“System.Data.SqlClient.SqlException”。 dll附加信息:参数化查询'(@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>