2017-01-09 2 views
0

データベース行から2つの列値を収集することになりました。 このメソッドは、2つではなく1つの値を取得するためにのみ機能します。セルから別の変数に値を保存する必要があります。次に、これらの変数を使用して別のデータベースにデータを格納します。保存方法2つの異なるセル値をデータベースから2つの異なる変数に置き換えます。C#

string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True"; 
using (var con2 = new SqlConnection(connectionString)) 
{ 
    try 
    { 
     con2.Open();    
     SqlCommand command = new SqlCommand(); 
     command.Connection = con2; 
     command.CommandText = string.Format("update Inventory set Quantity= Quantity - {0} WHERE id='"+tbItemid.Text+"'", Convert.ToInt32(tbQuantity.Text)); 
     command.ExecuteNonQuery(); 
     con2.Close(); 
     Data(); 
     DData(); 
     con2.Open(); 
     int x = int.Parse(tbQuantity.Text); 
     SqlCommand cmd1 = new SqlCommand("SELECT Model from Inventory WHERE id='" + tbItemid.Text + "'", con2); 
     SqlDataReader modelRdr = null; 
     modelRdr = cmd1.ExecuteReader(); 
     modelRdr.Read(); 
     modelRdr = cmd1.ExecuteReader(); 
     string model = modelRdr["model"].ToString(); 
     con2.Close(); 
     con.Open(); 
     int y = int.Parse(tbQuantity.Text); 
     SqlCommand cmd2 = new SqlCommand("SELECT Price from Inventory WHERE id='" + tbItemid.Text + "'", con2); 
     SqlDataReader pricerdr = null; 
     pricerdr = cmd2.ExecuteReader(); 
     pricerdr.Read(); 
     int price = int.Parse(pricerdr["Price"].ToString()); 
     SqlCommand cmd = con.CreateCommand(); 
     cmd.CommandType = CommandType.Text; 
     cmd.CommandText = "insert into Bill values (" + tbItemid.Text + ",'" +model.ToString()+ "',"+price.ToString()+",'"+tbQuantity.Text+"')"; 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Data(); 
    } 
    catch 
    { 
     MessageBox.Show("Enter Catagory and Product ID"); 
    } 
} 

答えて

1

まず最初最初のあなたはパラメータ化クエリ代わりの連結を使用する必要があります。これらの種類のクエリは、SQLインジェクションになりがちです。あなたは、パラメータを使用した完全なコードが

string model=String.Empty; 
int price = 0; 
string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Northwind;Integrated Security=True"; 
using (SqlConnection con2 = new SqlConnection(connectionString)) 
{ 
    try 
    { 
     con2.Open();    
     using(SqlCommand command = new SqlCommand()) 
     { 
      command.Connection = con2; 
      command.CommandText = string.Format("update Inventory set Quantity = Quantity - @qty WHERE [email protected]"; 
      command.Parameters.AddWithValue("@id", tbItemid.Text); 
      command.Parameters.AddWithValue("@qty", Convert.ToInt32(tbQuantity.Text))); 
      command.ExecuteNonQuery(); 

      Data(); 
      DData(); 

      int x = int.Parse(tbQuantity.Text); 
      using(SqlCommand cmd1 = new SqlCommand("SELECT Model, Price from Inventory WHERE [email protected]")) 
      { 
       cmd1.Parameters.AddWithValue("@id", tbItemid.Text); 
       SqlDataReader modelRdr = null; 
       modelRdr = cmd1.ExecuteReader(); 
       modelRdr.Read(); 
       model = modelRdr["model"].ToString(); 
       price = int.Parse(modelRdr["Price"].ToString());  
      } 
      using(SqlCommand cmd = con.CreateCommand()) 
      { 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = "insert into Bill values (@id,@model,@price,@qty)";. 
       cmd.Parameters.AddWithValue("@id", tbItemid.Text); 
       cmd.Parameters.AddWithValue("@model", model); 
       cmd.Parameters.AddWithValue("@price", price); 
       cmd.Parameters.AddWithValue("@qty", tbQuantity.Text); 
       cmd.ExecuteNonQuery(); 
      } 
      Data(); 
     } 
     catch 
     { 
      MessageBox.Show("Enter Catagory and Product ID"); 
     } 
    } 
} 
+0

追加情報のようになります。一つのコマンド

SqlCommand cmd1 = new SqlCommand("SELECT Model, Price from Inventory WHERE id='" + tbItemid.Text + "'", con2); SqlDataReader modelRdr = null; modelRdr = cmd1.ExecuteReader(); modelRdr.Read(); modelRdr = cmd1.ExecuteReader(); string model = modelRdr["model"].ToString(); int price = int.Parse(modelRdr["Price"].ToString()); 

で両方の列を読むことができます:最初に閉じる必要があります。このコマンドに関連付けられているオープンのDataReaderが既にあります。 –

+0

この例外は来ています –

+0

ああ申し訳ありませんが、私は非常にSQLとC#にあなたはそれが何であるか教えてくれますか(パラメータ化されたクエリ) –

関連する問題