private ArrayList GetPrimaryKeysBySQLPrimaryKey (string catalog, string schema, string table)
{
ArrayList keys = new ArrayList ();
IntPtr handle = IntPtr.Zero;
OdbcReturn ret;
try {
ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
command.Connection.hDbc, ref handle);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (
OdbcHandleType.Dbc, Connection.hDbc);
ret = libodbc.SQLPrimaryKeys (handle, catalog, -3,
schema, -3, table, -3);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
int length = 0;
byte [] primaryKey = new byte [255];
ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.CHAR, primaryKey, primaryKey.Length, ref length);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
while (true) {
ret = libodbc.SQLFetch (handle);
if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
break;
string pkey = Encoding.Default.GetString (primaryKey, 0, length);
keys.Add (pkey);
}
} finally {
if (handle != IntPtr.Zero) {
ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, handle);
if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
throw Connection.CreateOdbcException (OdbcHandleType.Stmt, handle);
}
}
return keys;
}