1

初心者注意!私は現在、Visual Studio で asp.net プロジェクトに取り組んでおり、以前の経験や知識はありません。セッション変数を含む where 句に従って更新するクエリを作成する必要があります。

整数値を格納する Session["patid"] があります。私のコードのクエリ部分は次のようになります。

SqlCommand cmd1 = new SqlCommand("update prescription set Medicine#1='"+med1txt.Text+"' where Patient_ID = Session["patid"]",con);

ここで、con は SqlConnection です。これだと「; expected」、「Invalid expression term ')'」などのビルドエラーがかなり発生します。また、Patient_ID は int 型の属性なので、Session["patid"].ToString( は使用できません。 )。誰でもこれに対する解決策を提案できますか? ありがとう!

4

2 に答える 2

0

これを行うためのすべてのベストプラクティス/セキュリティルールに違反していることを考慮して、以下を見てください:

SqlCommand cmd1 = new SqlCommand("update prescription set Medicine ='" + med1txt.Text + "' where Patient_ID =" + Session["patid"]",con);

SQLインジェクションなどを避けるために、少なくともSqlCommandとパラメータを使用します。

詳しくはこちらをご覧ください

于 2013-06-10T15:24:54.693 に答える
0

これを試して

String query = @"update prescription set medicine#1=@medicine where patient_id=@patientId";
using(SqlCommand cmd = new SqlCommand(query, conn)){
cmd.Parameters.AddWithValue("@medicine", med1txt.Text);
cmd.Parameters.AddWithValue("@patientId", Session["patid"]);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
于 2013-06-10T15:34:24.163 に答える