0

C# の Windows フォーム ボタンを使用してデータベースを更新しようとしています。これから表示するコードを実行してもエラーは表示されませんが、データベースまたはデータ グリッドのデータは変更されません。

private void update_Click(object sender, EventArgs e)
{
    SqlConnection NewCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ASPNET\cd\cdcwk2\cddatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlCommand cmd = new SqlCommand("UPDATE cdstock set cd_title='" + cdtext.Text + "', artist='" + artisttext.Text + "',genre='" + genretext.Text + "', rating=" + ratingtext.Text + ", cd_discription='" + distext.Text + "' where cd_id =" + idtext.Text + ";", NewCon);
    MessageBox.Show("UPDATE cdstock  set cd_title='" + cdtext.Text + "', artist='" + artisttext.Text + "',genre='" + genretext.Text + "', rating='" + ratingtext.Text + "', cd_discription='" + distext.Text + "' where cd_id ='" + idtext.Text + "'");

    NewCon.Open();

    cmd.ExecuteNonQuery();

    NewCon.Close();
    MessageBox.Show("Record Has Now Been UPDATED!!");

}
4

1 に答える 1

0

こんにちは、解決策を見つけました。今は問題なく動作しています。お時間をいただきありがとうございます。解決策は以下です。

             String NewCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ASPNET\cd\cdcwk2\cddatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

         SqlConnection con = new SqlConnection(NewCon);
         SqlCommand cmd = new SqlCommand();

         cmd.CommandText = "UPDATE cdstock set cd_title='" + cdtext.Text + "', artist='" + artisttext.Text + "',genre='" + genretext.Text + "', cd_discription='" + distext.Text + "', rating=" + ratingtext.Text + " where cd_id = " + dataGridView1.CurrentRow.Cells[0].Value;
         cmd.Connection = con;
         cmd.Connection.Open();

         cmd.ExecuteNonQuery();
         cmd.Connection.Close();

cddatabaseDataSet3.GetChanges(); this.cdstockTableAdapter.Fill(this.cddatabaseDataSet3.cdstock);

        MessageBox.Show("Record Has Now Been UPDATED!!");
于 2013-03-08T09:27:40.370 に答える