protected void LinkButtonContacts_Click(object sender, EventArgs e)
{
string obj1 = TextBox6.Text.Trim();
string obj2 = TextBox7.Text.Trim();
string obj3 = TextBox8.Text.Trim();
string queryUpdate = "Update User_Objectives SET Objective1= @Objective1 , Objective2=@Objective2 ,Objective3=@Objective3 WHERE Email='useremail'";
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=resume;Integrated Security=True";
SqlCommand cmd = new SqlCommand(queryUpdate, con);
cmd.Parameters.AddWithValue("@Objective1", TextBox6.Text);
cmd.Parameters.AddWithValue("@Objective2", TextBox7.Text);
cmd.Parameters.AddWithValue("@Objective3",TextBox8.Text );
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
Label1.Text = added + "record inserted";
}
catch (Exception err)
{
Label1.Text = "error inserting record";
Label1.Text = Label1.Text + err.Message;
}
finally
{
con.Close();
}
1 に答える
1
コード内のいくつかのこと:
Update
まず、レコードではなく、レコードを作成しようとしていINSERT
ます。useremail
2番目where句で渡されたものに基づいてレコードを更新しています。変数added
には影響のある行数が含まれているはずですが、検索パラメーターに基づいて更新するレコードがクエリで見つかりません。
現在 where 節に がありますWHERE Email='useremail'
。文字列リテラルを渡しています。代わりに、テーブルで更新されると予想される電子メール アドレスを渡します。残りのパラメーターで行っているように、そのために追加の SqlParameter を使用できます。
于 2013-03-19T07:40:29.360 に答える