2017-01-04 12 views
0

私はform1からform2への値を渡し、その値をwhere条件として使用しましたが、修正できないようです。私はテーブルbtwを更新しています。どんな助けでも大歓迎です。更新クエリと接続プロパティが初期化されていません

SqlConnection cn = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=inventory2;Integrated Security=True"); 
    SqlCommand cmd = new SqlCommand(); 
    SqlDataAdapter adptr = new SqlDataAdapter(); 
    DataSet dt = new DataSet(); 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (this.Text == "EDIT") 
     {   
      cmd.CommandText = string.Format("Update Items Set (Barcode='" + txtcode.Text + "' ,Productname= '" + txtitemname.Text + "',Prices= '" + txtPrices.Text + "' ,Quantity= '" + txtquantity.Text + "' ,Brand= '" + txtbrand.Text + "',Expiry= '" + txtexpiry.Text + "',Description='" + txtdescription.Text + "' ,Critical= '" + txtcritical.Text + "' where Barcode = '" + txtTry.Text + "')", cn);   
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Records Updated!"); 
      txtcode.Text = ""; 
      txtitemname.Text = ""; 
      txtPrices.Text = ""; 
      txtquantity.Text = ""; 
      txtbrand.Text = ""; 
      txtexpiry.Text = ""; 
      txtdescription.Text = ""; 
      txtcritical.Text = ""; 
     } 
     else 
     { 
      MessageBox.Show("Invalid"); 
     } 
+0

コマンドオブジェクトに代入するのではなく、String.Format呼び出しに接続を渡しています。 **文字列連結ではなくSqlParametersを使用してください。 – stuartd

+0

はい、病気のパラミターを使用しています。すでに "cmd.Connection = cn;"が追加されていますSystem.Data.dllに「System.Data.SqlClient.SqlException」という未処理例外が発生しました。 追加情報:executenonquery行の '('。)の構文が正しくありません。 – FutureDev

+0

そのエラーは、あなたのSQLが有効でないためです: 'set'グループはそれのまわりに括弧を入れてはいけません:' Update Items Set Barcode = '... – stuartd

答えて

0

私はエラーメッセージが十分にはっきりしていると思いますが、実行するコマンドに接続を割り当てる必要があります。しかし、ここであなたはもう一つの大きな問題、すなわちに直面する可能性があり、SQLインジェクションをこの連結クエリテキストクエリの、あなたがあなたのコードは次のようになりますでしょう要するに、注入を避けるために、代わりにパラメータを使用する必要があるため:。

string connectioStr = "Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=inventory2;Integrated Security=True"; 
string querySQL = "Update Items Set [email protected],[email protected],[email protected],[email protected] where Barcode = @condition"; 
// add more columns as you needed in the set 
using (SqlConnection conSQL = new SqlConnection(connectioStr)) 
{ 
    using (SqlCommand cmdSQL = new SqlCommand()) 
    { 
     cmdSQL.Connection = conSQL; 
     cmdSQL.CommandText = querySQL; 
     cmdSQL.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = txtcode.Text; 
     cmdSQL.Parameters.Add("@Productname", SqlDbType.VarChar).Value = txtitemname.Text; 
     cmdSQL.Parameters.Add("@Prices", SqlDbType.VarChar).Value = txtPrices.Text; 
     cmdSQL.Parameters.Add("@Quantity", SqlDbType.VarChar).Value = txtquantity.Text; 
     cmdSQL.Parameters.Add("@condition", SqlDbType.VarChar).Value = txtcode.Text; 
     // Add all parameters specified in the query 
     // use appropriate datatypes as per the type of columns 
    } 
} 

コマンドの初期化時にコマンドの接続とクエリを指定できます。この場合、コマンドの初期化は次のようになります。

SqlCommand cmdSQL = new SqlCommand(querySQL,conSQL); 
+0

ありがとうございました – FutureDev

関連する問題