私のコードから問題が残っています。私はSQLのデータベースと2つの名前のテーブルを持っています:カテゴリーとプロデュース。私はすでにviewByCatという名前のプロシージャを作成しました。このプロシージャは、コンボボックスに表示するカテゴリの種類を選択すると、すべてのカテゴリの1つを生成します。例えば、私はコンボボックスに服、靴、猫を持っています。「服」を選択すると、すべての服が生産に保存されています。どのように私は、手続きの入力として、コンボボックスのテキストを渡すことができ、私のコードを見てみましょう:プロシージャの入力としてコンボボックスのC#フォームからテキストを送信するSQL
SQLあなたのC#コードを更新する必要があり
create proc viewByCat
@keywords nvarchar(30)
as
select PD.ProID , PD.ProName , PD.TinyDes ,PD.FullDes ,PD.Price ,PD.CatID ,PD.Quantity
from Categories CT join Products PD on CT.CatID = PD.CatID
where CT.CatName = @keywords
go
C-シャープ
private void btn_viewByCategories_Click(object sender, EventArgs e)
{
//initialize source will be displayed on dgv
List<Produce> src_produces = new List<Produce>();
//create SqlConnection
//create connection
string connectionstring = @"server=.\SQLEXPRESS;database=sellingManager;integrated security=true";
//create SqlConnection with above string
using (SqlConnection cnn = new SqlConnection(connectionstring))
{
//create SqlCommand
SqlCommand cmd = cnn.CreateCommand();
//use SqlCommand to present for procedure
cmd.CommandType = CommandType.StoredProcedure;
//invoke procedure will be used
cmd.CommandText = "viewByCat";
//open connection
cnn.Open();
//
using (IDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
//something...
}
以下のようなストアドプロシージャにパラメータ値を渡すことができます。cmd.parameters.addwithvalue( "@ keywords"、dropdownselectecteditem); – user2173966
@ user2173966 dropdownselectecteditemを理解できません –