如题

解决方案 »

  1.   

    你装一个componentOne完整版,你会发现里面有很多操作数据库的源代码都是用的 odbc api,代码量大,而且也很复杂,如果用它我觉得真的是没有必要。
      

  2.   

    <!--startfragment-->*********************************
    odbc - open database connectivity
    *********************************basic stepsconnecting to the sql server database for retrieving information from tables
    *************************************************************
    the steps 1 - 3 are for connecting to the sql server database
    *************************************************************
    1. allocate odbc environment handleif sqlallocenv(glenv) <> 0 then
      msgbox "unable to initialize odbc api drivers!"
      end
    end if
    ______________________________________________________________2. allocate odbc database handledim istatus as integerif sqlallocconnect(glenv, gldbc) <> 0 then
      msgbox "could not allocate memory for connection handle!"
      odbcinit = false  ' free the environment
      istatus = sqlfreeenv(lenv)  if istatus = sql_error then
        msgbox "error freeing environment from odbc drivers"
      end if  ' quit the application
      end
    end if
    ______________________________________________________________ 3. connect using the sconnect string - sqldriverconnectdim sresult as string
    dim isize as integer
    dim sconnect as stringsconnect = "dsn=" & gsdsn & ";uid=" & gsloginid & ";pwd=" & gspassword & ";app=" & gsappcode & ";database=" & gsdatabaseif sqldriverconnect(gldbc, screen.activeform.hwnd, sconnect, len(sconnect), sresult, len(sresult), isize, 0) <= 0 then
      msgbox "could not establish connection to odbc driver!"
    end if
    ______________________________________________________________
    ***************************************************
    the steps 4 - 8 are for retrieving data from tables
    ***************************************************
    4. allocate odbc statement handleif sqlallocstmt(gldbc, glstmt) <> 0 then   msgbox "could not allocate memory for a statement handle!"end if
    ______________________________________________________________5. execute odbc statement - sqlexecdirectdim lret as long, lerrno as long
    dim ilen as integer
    dim ssqlstate as string * max_data_buffer
    dim serrormsg as string * max_data_buffer
    dim smsg as stringssql = "select name, location from authors"if sqlexecdirect(glstmt, ssql, len(ssql)) <> sql_success then
      ' also check for odbc error message - sqlerror
      lret = sqlerror(glenv, gldbc, glstmt, ssqlstate, lerrno, serrormsg, max_data_buffer, ilen)
      smsg = "error executing sql statement" & chr$(13) & chr$(10)
      smsg = smsg & "odbc state = " & trim$(left$(ssqlstate, instr(ssqlstate, chr$(0)) - 1)) & chr$(13) & chr$(10)
      smsg = smsg & "odbc error message = " & left$(serrormsg, ilen)
      msgbox smsg, vbinformation, "execute query"
    end if
    ______________________________________________________________6. fetch one row of results from executed odbc statement - sqlfetchcode in step 7.
    ______________________________________________________________7. get the data in each field of the fetched row - sqlgetdatadim bperform as integer, istatus as integer
    dim sdata as string * max_data_buffer
    dim loutlen as longbperform = sqlfetch(glstmt)do while bperform
      bperform = sqlfetch(lstmt)     ' get the next row of data
      if bperform = sql_success then    ‘ if rows of data available   
        bperform = true    ' get author name - icolumn = 1 for first field i.e. name in ssql 
        istatus = sqlgetdata(glstmt, icolumn, 1, sdata, max_data_buffer, loutlen)    ' loutlen = length of the valid data in sdata
        ' data value will be = left$(sdata, loutlen), loutlen = -1 if no data or null data    ' get location - icolumn = 2 for second field i.e. location in ssql
        istatus = sqlgetdata(glstmt, icolumn, 1, sdata, max_data_buffer, loutlen)     ' add the field data to correponding data display controls for this row
      else
        bperform = false  ' no more rows available
      end if
    loop'release the odbc statement handle
    bperform = sqlfreestmt(glstmt, sql_drop)
    ______________________________________________________________8. release the odbc statement handle - sqlfreestmtcode in step 7.
    ______________________________________________________________
    *******************************************************************
    the steps 9 - 11 are for disconnecting from the sql server database
    *******************************************************************
    9. disconnect from odbc database - sqldisconnectistatus = sqldisconnect(gldbc)
    ______________________________________________________________10. release the odbc database handle - sqlfreeconnectistatus = sqlfreeconnect(gldbc)
    ______________________________________________________________
      

  3.   

    11. release the odbc environment handle - sqlfreeenvistatus = sqlfreeenv(glenv)
    ______________________________________________________________ ***********************************************************************
    the following entries are required in the odbcapi module
    ***********************************************************************
    ' odbc variables and constants
     
    global glenv as long
    global gldbc as long
    global ssql as string
     
    global const max_data_buffer = 255
    global const sql_success = 0
    global const sql_success_with_info = 1
    global const sql_error = -1
    global const sql_no_data_found = 100
    global const sql_close = 0
    global const sql_drop = 1
    global const sql_char = 1
    global const sql_numeric = 2
    global const sql_decimal = 3
    global const sql_integer = 4
    global const sql_smallint = 5
    global const sql_float = 6
    global const sql_real = 7
    global const sql_double = 8
    global const sql_varchar = 12
    global const sql_data_source_name = 6
    global const sql_user_name = 8
    'odbc declarations
    'the hwnd is a long in windows 95 & windows nt#if win32 then
      declare function sqlallocenv lib "odbc32.dll" (env as long) as integer
      declare function sqlfreeenv lib "odbc32.dll" (byval env as long) as integer
      declare function sqlallocconnect lib "odbc32.dll" (byval env as long, ldbc as long) as integer
      declare function sqlconnect lib "odbc32.dll" (byval ldbc as long, byval server as string, _
                                 byval serverlen as integer, byval uid as string, _
                                 byval uidlen as integer, byval pwd as string, _
                                 byval pwdlen as integer) as integer  declare function sqldriverconnect lib "odbc32.dll" (byval ldbc as long, byval hwnd as long, _
                                    byval szcsin as string, byval cbcsin as integer, _
                                    byval szcsout as string, byval cbcsmax as integer, _
                                    cbcsout as integer, byval f as integer) as integer  declare function sqlfreeconnect lib "odbc32.dll" (byval ldbc as long) as integer
      declare function sqldisconnect lib "odbc32.dll" (byval ldbc as long) as integer
      declare function sqlallocstmt lib "odbc32.dll" (byval ldbc as long, lstmt as long) as integer
      declare function sqlfreestmt lib "odbc32.dll" (byval lstmt as long, byval endoption as integer) as integer
      declare function sqltables lib "odbc32.dll" (byval lstmt as long, byval q as long, _
                                byval cbq as integer, byval o as long, _
                                byval cbo as integer, byval t as long, _
                                byval cbt as integer, byval tt as long, _
                                byval cbtt as integer) as integer  declare function sqlexecdirect lib "odbc32.dll" (byval lstmt as long, byval sqlstring as string, _
                                   byval sqlstrlen as long) as integer  declare function sqlnumresultcols lib "odbc32.dll" (byval lstmt as long, numcols as integer) as integer
      declare function sqldescribecol lib "odbc32.dll" (byval lstmt as long, byval colnum as integer, _
                                   byval colname as string, byval buflen as integer, _
                                   colnamelen as integer, dtype as integer, _
                                   dl as long, ds as integer, n as integer) as integer  declare function sqlfetch lib "odbc32.dll" (byval lstmt as long) as integer
      declare function sqlgetdata lib "odbc32.dll" (byval lstmt as long, byval col as integer, _
                                 byval wconvtype as integer, byval lpbbuf as string, _
                                 byval dwbuflen as long, lpcbout as long) as integer  declare function sqlgetinfo lib "odbc32.dll" (byval ldbc as long, byval hwnd as long, _
                                 byval szinfo as string, byval cbinfomax as integer, _
                                 cbinfoout as integer) as integer
      

  4.   

    declare function sqlerror lib "odbc32.dll" (byval env as long, byval ldbc as long, _
                                byval lstmt as long, byval sqlstate as string, _
                                nativeerror as long, byval buffer as string, _
                                byval buflen as integer, outlen as integer) as integer#else  declare function sqlallocenv lib "odbc.dll" (env as long) as integer
      declare function sqlfreeenv lib "odbc.dll" (byval env as long) as integer
      declare function sqlallocconnect lib "odbc.dll" (byval env as long, ldbc as long) as integer
      declare function sqlconnect lib "odbc.dll" (byval ldbc as long, byval server as string, _
                                byval serverlen as integer, byval uid as string, _
                                byval uidlen as integer, byval pwd as string, _
                                byval pwdlen as integer) as integer  declare function sqldriverconnect lib "odbc.dll" (byval ldbc as long, byval hwnd as integer, _
                                   byval szcsin as string, byval cbcsin as integer, _
                                   byval szcsout as string, byval cbcsmax as integer, _
                                   cbcsout as integer, byval f as integer) as integer  declare function sqlfreeconnect lib "odbc.dll" (byval ldbc as long) as integer
      declare function sqldisconnect lib "odbc.dll" (byval ldbc as long) as integer
      declare function sqlallocstmt lib "odbc.dll" (byval ldbc as long, lstmt as long) as integer
      declare function sqlfreestmt lib "odbc.dll" (byval lstmt as long, byval endoption as integer) as integer
      declare function sqltables lib "odbc.dll" (byval lstmt as long, byval q as long, _
                               byval cbq as integer, byval o as long, _
                               byval cbo as integer, byval t as long, _
                               byval cbt as integer, byval tt as long, _
                               byval cbtt as integer) as integer  declare function sqlexecdirect lib "odbc.dll" (byval lstmt as long, byval sqlstring as string, _
                                 byval sqlstrlen as long) as integer  declare function sqlnumresultcols lib "odbc.dll" (byval lstmt as long, numcols as integer) as integer
      declare function sqldescribecol lib "odbc.dll" (byval lstmt as long, byval colnum as integer, _
                                  byval colname as string, byval buflen as integer, _
                                  colnamelen as integer, dtype as integer, dl as long, _
                                  ds as integer, n as integer) as integer  declare function sqlfetch lib "odbc.dll" (byval lstmt as long) as integer
      declare function sqlgetdata lib "odbc.dll" (byval lstmt as long, byval col as integer, _
                                byval wconvtype as integer, byval lpbbuf as string, _
                                byval dwbuflen as long, lpcbout as long) as integer  declare function sqlgetinfo lib "odbc.dll" (byval ldbc as long, byval hwnd as integer, _
                                byval szinfo as string, byval cbinfomax as integer, _
                                cbinfoout as integer) as integer  declare function sqlerror lib "odbc.dll" (byval env as long, byval ldbc as long, _
                              byval lstmt as long, byval sqlstate as string, _
                              nativeerror as long, byval buffer as string, _
                              byval buflen as integer, outlen as integer) as integer#end if  
    建议还是使用ado