私は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");
}
コマンドオブジェクトに代入するのではなく、String.Format呼び出しに接続を渡しています。 **文字列連結ではなくSqlParametersを使用してください。 – stuartd
はい、病気のパラミターを使用しています。すでに "cmd.Connection = cn;"が追加されていますSystem.Data.dllに「System.Data.SqlClient.SqlException」という未処理例外が発生しました。 追加情報:executenonquery行の '('。)の構文が正しくありません。 – FutureDev
そのエラーは、あなたのSQLが有効でないためです: 'set'グループはそれのまわりに括弧を入れてはいけません:' Update Items Set Barcode = '... – stuartd