2016-08-02 2 views
0

선택한 행을 DataGridView에서 삭제 한 후 MySQL 데이터베이스를 업데이트 할 수 없습니다. ExecuteNonQuery()을 실행하면 다음과 같은 오류가 표시됩니다.MySQL 데이터베이스의 선택된 Datagridview에서 행을 삭제할 때의 문제

System.Invalid.Operation.Exception : Connection이 유효하고 열려 있어야합니다.

그러나 올바르게 연결되었습니다. 아직도 문제가 생겼어.

내 코드는 다음과 같습니다 :

private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    String^ constring = L"datasource=localhost;port=3306;username=****;password=********"; 
    MySqlConnection^ conDataBase = gcnew MySqlConnection(constring); 
    conDataBase->Open(); 
    try 
    { 
     if (MessageBox::Show("Sure you wanna delete?", "Warning", MessageBoxButtons::YesNo) == System::Windows::Forms::DialogResult::Yes) 
     { 
      for each(DataGridViewCell^ oneCell in dataGridView1->SelectedCells) 
      { 
       if (oneCell->Selected) { 
        dataGridView1->Rows->RemoveAt(oneCell->RowIndex); 
        MySqlCommand^ cmdDataBase1 = gcnew MySqlCommand("Delete from Dinslaken_DB.Configuration where Memory=" + dataGridView1->CurrentRow->Index +""); 
        cmdDataBase1->ExecuteNonQuery(); 
        //sda->Update(dbdataset); 
       } 
      }   
     } 
    } 
    catch (Exception^ex) 
    { 
     MessageBox::Show(ex->ToString()); 
    } 
} 
+0

잠시 시간을내어 도움말 센터에서 [편집 도움말] (// stackoverflow.com/editing-help)을 읽어보십시오. 스택 오버플로에서 서식 지정은 다른 사이트와 다릅니다. 게시물이 좋아지면 다른 사람들이 읽고 이해하는 것이 더 쉬워집니다. – FrankerZ

+0

.NET의 경우 C++ 대신 C#을 사용하십시오. C++ .net의 구문은 매우 추합니다. 처벌하지 마라. – i486

답변

0

당신은 명령 개체

MySqlCommand^ cmdDataBase1 = gcnew MySqlCommand("Delete from Dinslaken_DB.Configuration where Memory=" + dataGridView1->CurrentRow->Index +""); 

을 만들었습니다하지만 당신은 그 명령의 속성을 설정하지 않았습니다. 위의 명령 생성자에 대한 두 번째 인수는 연결이어야합니다. 그것을 확인하십시오.

그래서 명령 생성자가 있어야한다 :

MySqlCommand^ cmdDataBase1 = gcnew MySqlCommand("Delete from Dinslaken_DB.Configuration where Memory=" + dataGridView1->CurrentRow->Index +"", conDataBase); 

는 또한 연결을 종료하는 것을 잊지 마세요.

+0

고마워, 나는 그것을 발견하고 그것이 효과가있다. –

관련 문제