2011-08-02 5 views
0

C# 및 db 프로그래밍에 익숙하지 않습니다. 그리고 다른 사람의 코드를 인계했습니다. DB를 업데이트하려고 할 때 오류가 발생합니다. 여기 코드는 다음과 같습니다DB 오류 받기 : 조건 식의 데이터 형식이 일치하지 않습니다. C#

private void EnableEvent(int eventID) 
    { 


     OleDbCommand oleCMD = new OleDbCommand(); 
     oleCMD.Connection = Database.SqlConn(); 
     OleDbTransaction oleTrans = oleCMD.Connection.BeginTransaction(); 
     oleCMD.Transaction = oleTrans; 

     try 
     { 
      StringBuilder sql = new StringBuilder(); 
      sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID='{0}'", eventID); 

      oleCMD.CommandText = sql.ToString(); 
      // insert the header 
      oleCMD.ExecuteNonQuery(); 
      oleTrans.Commit(); 
     } 
     catch(Exception e) 
     { 
      MessageBox.Show(e.Message, "Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
     } 
     finally 
     { 
      oleCMD.Connection.Close(); 
      oleCMD.Dispose(); 
     } 
    } 
+0

전체 오류 문자열을 게시 할 수 있습니까? – raym0nd

+0

더 이상 오류 문자열을 얻으려면 어떻게해야합니까? e.message가 "조건 식의 데이터 형식 불일치"입니다. – Matt

답변

0

편집 :

잡았다 지금 ... 아래와 같은 쿼리를 포맷하고 확실하게 Numeric 필드 yiou 아포스트로피를 넣으면 안됩니다으로

 StringBuilder sql = new StringBuilder(); 
     sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID = {0}", eventID); 

를 작동합니다 값 주위에. 그것이 문제였습니다.

+0

eventID가 int입니다 ... 두 번 확인했습니다. – Matt

+0

내 대답에서 제안한 쿼리 형식을 시도 했습니까? set isActive = '1' '을 인용하고 한 번 시도하십시오. 잘 작동해야합니다. – Rahul

+0

나는 그랬다. 불운. MySQL Workbench에서 수동으로 시도 할 수있는 명령어는 무엇입니까? 데이터베이스에서 SELECT 쿼리를 실행하는 방법을 알고 있습니다. 하지만 어떻게 업데이트를 실행합니까? 이 방법을 통해 데이터베이스가 업데이트를 수행하는지 확인할 수 있습니다. – Matt

0

시험해보세요.

private void EnableEvent(int eventID) 
{ 
    OleDbConnection myConn = new OleDbConnection(myConnString); 
    myConn.Open(); 

    OleDbCommand myCommand = myConn.CreateCommand(); 
    OleDbTransaction myTrans; 
    // Start a local transaction 
    myTrans = myConn.BeginTransaction(); 
    // Assign transaction object for a pending local transaction 
    myCommand.Connection = myConn; 
    myCommand.Transaction = myTrans; 

    try 
    { 
     StringBuilder sql = new StringBuilder(); 
     sql.AppendFormat("UPDATE Events SET isActive = 1 where EventID='{0}'", eventID); 

     myCommand.CommandText = sql.ToString(); 
     // insert the header 
     myCommand.ExecuteNonQuery(); 
     myTrans.Commit(); 
    } 
    catch(Exception e) 
    { 
     MessageBox.Show(e.Message, "Database Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
    } 
    finally 
    { 
     myCommand.Connection.Close(); 
     myCommand.Dispose(); 
    } 
} 

이 정보가 도움이되는지 알려주세요.

관련 문제