データベースからデータを保存および更新するプログラムを作成します。データを保存および読み取り、更新することもできますが、問題は、"ID"
インデックスとして を選択できないことです。インデックスとして使用するサンプル コードを"ID"
次に示します。
cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Records SET FirstName = @firstname, LastName = @lastname, Age = @age, Address = @address, Course = @course WHERE [ID] = @id";
cmd.Parameters.AddWithValue("@id", int.Parse(label7.Text));
cmd.Parameters.AddWithValue("@firstname", textBox1.Text);
cmd.Parameters.AddWithValue("@lastname", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.Parameters.AddWithValue("@address", textBox4.Text);
cmd.Parameters.AddWithValue("@course", textBox5.Text);
cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery();
{
MessageBox.Show("Update Success!");
cn.Close();
}
これが機能する私の更新コードですが、インデックスは"firstname"
、
cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Records SET FirstName = @firstname, LastName = @lastname, Age = @age, Address = @address, Course = @course WHERE FirstName = @firstname";
//cmd.Parameters.AddWithValue("@id", int.Parse(label7.Text));
cmd.Parameters.AddWithValue("@firstname", textBox1.Text);
cmd.Parameters.AddWithValue("@lastname", textBox2.Text);
cmd.Parameters.AddWithValue("@age", textBox3.Text);
cmd.Parameters.AddWithValue("@address", textBox4.Text);
cmd.Parameters.AddWithValue("@course", textBox5.Text);
cmd.Connection = cn;
cn.Open();
cmd.ExecuteNonQuery();
{
MessageBox.Show("Update Success!");
cn.Close();`
}
動作しますが、問題は を更新できないこと"FirstName"
です。Firstname も更新できる方法はありますか? または"ID"
インデックスとして使用しますか?ありがとう