Title is No Title

not very good here!

导航

Use ODBC directy to get ID and give bigger.

CString Common::getObjId(CString table,CString field){
 
 CString ret;
 CString tableN=table;//"test";
 tableN+=";";
    unsigned char sql[100];
 char sqlH[100]="select ID from ";
 
    char *tableNC=tableN.GetBuffer(tableN.GetLength());
    strcat(sqlH,tableNC);
 for(int i=0;i<100;i++){
    sql[i]=(unsigned char)sqlH[i];
    }

 

 //turn ; into \0 as str's end;
 unsigned char *p=sql;
 while(*p){
 if(*p==';')
  *p='\0';
 p++;
 }
   //now should get right table.
    //MessageBox((char*)sql,"",MB_OK);
 //unsigned char *pT=table.GetBuffer(12);
  
 unsigned char *pSql=sql;

  RETCODE rcode;

  HENV henv1;
  HDBC hdbc1;
  HSTMT hstmt1;

 //char szFirstName[50];
 char nowValue[10];
 //char szPhoneNum[20];
   SDWORD sdODataLength;
    unsigned char conStringOut[256];
    //1:
    rcode = ::SQLAllocEnv(&henv1);
    if (rcode != SQL_SUCCESS) {
  AfxMessageBox("allocEnvError.");
     return "-1";
 }
    if (rcode == SQL_SUCCESS)
    { //2:
      rcode = ::SQLAllocConnect(henv1, & hdbc1);
      if (rcode != SQL_SUCCESS) {
  AfxMessageBox("AllocConnectError.");
  return "-2";
   }
   if (rcode == SQL_SUCCESS)
      {   //3:
          rcode = ::SQLDriverConnect(hdbc1, 0,
          (unsigned char *)"DSN=JiSheng",
          SQL_NTS, conStringOut, 256, NULL,
          SQL_DRIVER_NOPROMPT);
    if (rcode != SQL_SUCCESS) {
    AfxMessageBox("DroverCpmmectEror.");
       return "-3";
    }
         if (rcode == SQL_SUCCESS)
   {//4:
          rcode = ::SQLAllocStmt(hdbc1, &hstmt1);
    if (rcode != SQL_SUCCESS) {
    AfxMessageBox("SQLAllocStmtEror.");
       return "-4";
    }
          if (rcode == SQL_SUCCESS)
    {   //5:execute!
              rcode = ::SQLExecDirect(hstmt1,
             //(unsigned char *)
             // "SELECT * FROM test",
    //sql,
    //(unsigned char*)"select id from test",
    pSql,
              SQL_NTS);
             //6:get result by return code.
     long nowId=0;
            for (rcode = ::SQLFetch(hstmt1);
              rcode == SQL_SUCCESS;
              rcode = SQLFetch(hstmt1))
            {
              ::SQLGetData(hstmt1, 1, SQL_C_CHAR,
                nowValue, 50,  & sdODataLength);
     long tempL=atol(nowValue);
     if(tempL>nowId)
      nowId=tempL;
     //end set the now get bigger id.
              //::MessageBox(NULL, szLastName,
              //  " from AddressBookDb ", MB_OK);
            }//end for and get get biggest.
     nowId++;
             //CString temp;
    ret.Format("%d",nowId);
            
    //temp.Format("now id:%d,ret id:%d",nowId,nowId+1);
    //AfxMessageBox(temp);
            ::SQLFreeStmt(hstmt1, SQL_DROP);
          }
          ::SQLDisconnect(hdbc1);
        }
        ::SQLFreeConnect(hdbc1);
      }
      ::SQLFreeEnv(henv1);
    }
 return ret;
}

posted on 2004-03-21 18:39  abraham  阅读(713)  评论(0编辑  收藏  举报