2010-02-09 4 views
0

최근 MySQL 데이터 어댑터를 v5에서 v6으로 업그레이드했습니다. v5 MySQL 데이터 어댑터를 사용할 때 아래 코드는 완벽하게 작동했습니다. 그러나 데이터 어댑터의 v6을 사용하기 시작한 이래로 새 레코드를 추가하면 레코드의 "새로운"ID가 반환되지 않습니다. 누구든지 이것을 달성하기위한 우아한 방법을 알고 있습니까?MySQL 데이터 어댑터 버전 6.1.3.0이 ver 5 데이터 어댑터 작동시 ID를 반환하지 않습니다.

protected void SetRecord(ref T Data, string Sql) 
    { 
     // 
     // Get a connection to use 
     // 
     string connectionKey = ""; 
     MySqlConnection Connection = ConnectionManager.Current.OpenConnection(out connectionKey); 

     MySqlDataAdapter da; 
     MySqlCommandBuilder cb; 
     DataTable dt = new DataTable(); 
     DataRow dr; 

     da = new MySqlDataAdapter(Sql, Connection); 
     da.Fill(dt); 
     // 
     // If there is more than one row, then edit 1st row, else add new row 
     // 
     if (dt.Rows.Count > 0) 
     { 
      dr = dt.Rows[0]; 
      DatabaseRow_Set(ref dr, Data); 

      // 
      // Update the DataSet with the Database 
      // 
      cb = new MySqlCommandBuilder(da); 
      da.Update(dt); 

     } 
     else 
     { 
      dr = dt.NewRow(); 
      DatabaseRow_Set(ref dr, Data); 
      dt.Rows.Add(dr); 

      // 
      // Update the DataSet with the Database 
      // 
      cb = new MySqlCommandBuilder(da); 
      da.Update(dt); 

      // 
      // Call get from data row to get new ID 
      // 
      DatabaseRow_Get(dr, ref Data); 
     } 

     // 
     // Close Connection 
     // 
     ConnectionManager.Current.CloseConnection(connectionKey); 
    } 

답변

0

이 기능이 v5에서 작동하고 v6에서 작동하지 않는 이유를 알 수 없습니다. 그러나 해결 방법을 찾았습니다.

   dr = dt.NewRow(); 
       DatabaseRow_Set(ref dr, Data); 
       dt.Rows.Add(dr);     
       // 
       // Update the DataSet with the Database 
       // 
       cb = new MySqlCommandBuilder da);             
       da.Update(dt);     

       MySqlCommand cmd = new MySqlCommand("select LAST_INSERT_ID()", Connection); 
       long ID = ((long) cmd.ExecuteScalar()); 
       DatabaseRow_GetID(ref Data, ID);     
관련 문제