Board index » delphi » URGENT - Problem wrapping direct ODBC calls in D3

URGENT - Problem wrapping direct ODBC calls in D3

Hi,

I am currently working on a project to replace the BDE in a large
application with a wrapper around ODBC 2.0 and customized TDataSet
descendants.

I have managed without difficulties up to a point, but now an issue of
Object Pascal syntax is preventing me from moving forward.

My question is how to correctly return values from a result set obtained
using SQLExecDirect(hstmt). I have tried (unsuccessfully) to use SQLBindCol
to return CHAR values from a 1-column test table. The reason I'm having no
success is the rgbValue PTR parameter - I really don't know how to use this
without causing access violations.

To date I've tried something like this :

procedure TForm1.DoIt;
const
  COL1_LEN = 256; {Phyisical length og col_1 column in testtable}
var
  SQLResult : SQLRETURN; {Defined as SmallInt}
  szCOL1 : PChar;
  cbCOL1 : Integer;
begin
    try
        GetMem(szCOL1, COL1_LEN);
        SQLAllocEnv(MyEnv);
        SQLSllocConnect(MyEnv, MyHDBC);
        SQLConnect(MyHDBC, PChar('Oracle73'), 8, PChar('TestUID'), 7,
PChar('TestPWord'), 9);
        SQLAllocStmt(MyHDBC, MyStmt);
        SQLResult := SQLExecDirect(MyHSTMT, 'select col_1 from testtable',
SQL_NTS);
        SQLBindCol(MyHSTMT, 1, SQL_C_CHAR, @szCOL1, COL1_LEN, cbCOL1);
        SQLFetch(MyHSTMT); // ACCESS VIOLATION OCCURS HERE !!!!!
    finally
        FreeMem(szCOL1, COL1_LEN);
    end;
end;

If anyone has a sample of code that would work (showing the correct syntax
for the rbgValue buffer pointer and how to implement it - I would be
extremely grateful.

Many thanks in advance

Frank Pottle

 

Re:URGENT - Problem wrapping direct ODBC calls in D3


Frank M.V. Pottle <fpk.softw...@dial.pipex.com> wrote in article
<01bd818c$c50537e0$2bf582c1@panther>...

Quote
> Hi,

> I am currently working on a project to replace the BDE in a large
> application with a wrapper around ODBC 2.0 and customized TDataSet
> descendants.

Let me say again that I have never wrapped the ODBC API from
Delphi.  I have done it from C++ years ago -- so I have a little
bit of understanding.

Quote
> To date I've tried something like this :

> procedure TForm1.DoIt;
> const
>   COL1_LEN = 256; {Phyisical length og col_1 column in testtable}
> var
>   SQLResult : SQLRETURN; {Defined as SmallInt}
>   szCOL1 : PChar;
>   cbCOL1 : Integer;
> begin
>     try
>    GetMem(szCOL1, COL1_LEN);
>    SQLAllocEnv(MyEnv);
>    SQLSllocConnect(MyEnv, MyHDBC);
>    SQLConnect(MyHDBC, PChar('Oracle73'), 8, PChar('TestUID'), 7,
> PChar('TestPWord'), 9);
>    SQLAllocStmt(MyHDBC, MyStmt);
>    SQLResult := SQLExecDirect(MyHSTMT, 'select col_1 from testtable',
> SQL_NTS);
>    SQLBindCol(MyHSTMT, 1, SQL_C_CHAR, @szCOL1, COL1_LEN, cbCOL1);
>    SQLFetch(MyHSTMT); // ACCESS VIOLATION OCCURS HERE !!!!!
>     finally
>    FreeMem(szCOL1, COL1_LEN);
>     end;
> end;

I see what I think are several problems.  Not having the declaration
that you are using for SQLBindCol, I am partially speculating.  If
you could show me your declaration for SQLBindCol, I might be able
to do a little better than to speculate.

Quote
>    SQLBindCol(MyHSTMT, 1, SQL_C_CHAR, @szCOL1, COL1_LEN, cbCOL1);

First, the 4th argument (the @szCol1) is defined in the API as
a simple pointer argument.  If you declared it as such, passing @szCol1
is incorrect -- it should be just szCol1.  @szCol1 is a pointer to a
pointer.  This is the most likely cause of your problems.

Next, the cbCol1 argument (the last one) is also defined as a pointer
to an integer argument.  If you declared it as a 'var' argument, the
code as written is correct.  If you declared it as a pointer, it is
wrong.  It should be @cbCol1 (if the arg is declared as pointer by
value).

Also, if my memory serves, the max argument (COL1_LEN above) should
include space for the null terminator for SQL_C_CHAR type columns.

Hope this helps,
William
--
William Tucker
Compu-Share, Inc.

WTuc...@Compu-Share.com
William.Tuc...@worldnet.att.net

Other Threads