2016-11-07 5 views
0

私のコードから問題が残っています。私は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... 
      } 
+0

以下のようなストアドプロシージャにパラメータ値を渡すことができます。cmd.parameters.addwithvalue( "@ keywords"、dropdownselectecteditem); – user2173966

+0

@ user2173966 dropdownselectecteditemを理解できません –

答えて

0

。私はそれぞれの代わりのSQLConnectionとSqlCommandオブジェクトのMySqlConnection and MySqlCommandを使用することをお勧めいたしますでしょう

 SqlCommand cmd = new SqlCommand("viewByCat]", connectionstring); 
     cmd.CommandType = CommandType.StoredProcedure; 

     cmd.Parameters.Add("@keywords", SqlDbType.NVarChar).Value ="Your Selected value from combo box"; 


     cnn.Open(); 
     DataTable dt = new DataTable(); 
     using (SqlDataAdapter sdadptr = new SqlDataAdapter(cmd)) 
     { 
      sdadptr.Fill(dt); 
     } 
     cnn.Close(); 
     return dt; 
0

:次のコードを参照してください。

MySqlConnection conn = new MySqlConnection(connectionstring); 
MySqlCommand cmd = new MySqlCommand(); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.CommandText = "viewByCat"; 
cmd.Parameters.AddWithValue("@keywords", ComboBox.Text); 
cmd.Parameters["@keywords"].Direction = ParameterDirection.Input; 
MySqlDataReader myReader; 
myReader = cmd.ExecuteReader(); 
try 
{ 
    while(myReader.Read()) 
    { 
     Console.WriteLine(myReader.GetString(0)); 
    } 
} 
finally 
{ 
    myReader.Close(); 
    myConnection.Close(); 
} 
0

SPでパラメータを渡す必要があります。

using (SqlConnection cnn = new SqlConnection(connectionstring)) 
      { 
       DataSet ds = new DataSet(); 
       SqlDataAdapter da = new SqlDataAdapter(); 

       if (cnn.State.ToString() == "Closed") cnn.Open(); 

       SqlCommand cmd = new SqlCommand("viewByCat", cnn); 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.Parameters.Add("@keywords", SqlDbType.VarChar, 30).Value = Combobox.Text; 

       da.SelectCommand = cmd; 
       da.Fill(ds, "Search"); 

       cnn.Close(); 

      } 
0

C#コードで次のステートメントを追加するだけで済みます。

cmd.Parameters.AddWithValue("@keywords", YourComboBoxId.SelectedItem.Text); 
cmd.Parameters["@keywords"].Direction = ParameterDirection.Input; 

'YourComboBoxId'を実際のコンボボックスID値で変更します。これがあなたの問題を解決することを願っています。

関連する問題