Im still experimenting with databases and ftn95/.net
Now i can read all data out of a datatable with the help of a C#-support class and i can convert the field-contents to fortran data with the help of conversion functions written in C#. But I can not do the conversion with the cast@ function in fortran. Casting to Integer brings strange values, casting to real causes memory access violations.
In the example code the not working fortran version is commented out (in the select case statement).
Why does this not work?
Fortran Source:
program ReadMDB
library ('System') library ('System.Data') library('ItemSelector.ItemSelector')
implicit none
object('System.Data.OleDb.OleDbCommand') :: myCommand object('System.Data.OleDb.OleDbDataAdapter') :: myDataAdapter object('System.Data.OleDb.OleDbConnection') :: myConn object('System.Data.DataSet') :: myDataSet object('System.Data.DataTableCollection') :: tabs object('System.Data.DataRowCollection') :: rows object('System.Data.DataColumnCollection') :: cols object('System.Data.DataTable') :: myTab object('System.Data.DataRow') :: myRow object('System.Data.DataColumn') :: myCol object :: myObj object('System.Int16') :: myInt16 object('System.Int32') :: myInt32 object('System.Single') :: mySingle object('System.Double') :: myDouble string :: strConn,strSelect integer(kind=3) :: res,nrows,ncols,r,c integer(kind=2) :: shortvar integer(kind=3) :: longvar real(kind=1) :: singlevar real(kind=2) :: doublevar character(len=255) :: otype,fldname,charvar
assembly_external(name='System.Object.ToString') otostring assembly_external(name='System.Data.OleDb.OleDbConnection.Open') dc_open assembly_external(name='System.Data.OleDb.OleDbConnection.Close') dc_close assembly_external(name='System.Data.OleDb.OleDbDataAdapter.Fill') da_fill
assembly_external(name='ItemSelector.ItemSelector.GetTable') gettable assembly_external(name='ItemSelector.ItemSelector.GetRow') getrow assembly_external(name='ItemSelector.ItemSelector.GetColumn') getcolumn assembly_external(name='ItemSelector.ItemSelector.ObjectToShort') toshort assembly_external(name='ItemSelector.ItemSelector.ObjectToLong') tolong assembly_external(name='ItemSelector.ItemSelector.ObjectToSingle') tosingle assembly_external(name='ItemSelector.ItemSelector.ObjectToDouble') todouble assembly_external(name='ItemSelector.ItemSelector.ObjectToString') tostring
strConn = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Test.mdb' strSelect = 'SELECT * FROM tblTest'
myConn = new@('System.Data.OleDb.OleDbConnection',strConn) myCommand = new@('System.Data.OleDb.OleDbCommand',strSelect,myConn) myDataAdapter = new@('System.Data.OleDb.OleDbDataAdapter',myCommand) myDataSet = new@('System.Data.DataSet')
res=da_fill(myDataAdapter,myDataSet,'Test')
tabs=MyDataset%Tables mytab=gettable(tabs,0) nrows=mytab%rows%count ncols=mytab%columns%count
rows=mytab%rows cols=mytab%columns
do r=0,nrows-1
print , 'Case: ', r
myrow=getrow(rows,r)
do c=0,ncols-1
mycol=getcolumn(cols,c)
fldname=mycol%caption
myobj=myrow%itemarray(c)
otype=otostring(myobj)
select case (otype)
case ('System.Int16')
shortvar=toshort(myobj)
! myInt16=cast@(myobj,'System.Int16')
! shortvar=myInt16
write(,100) fldname,otype, shortvar
case ('System.Int32')
longvar=tolong(myobj)
! myInt32=cast@(myobj,'System.Int32')
! longvar=myInt32
write(,100) fldname,otype, longvar
case ('System.Single')
singlevar=tosingle(myobj)
! mySingle=cast@(myobj,'System.Single')
! singlevar=mySingle
! write(,100) fldname,otype, singlevar
case ('System.Double')
doublevar=todouble(myobj)
! myDouble=cast@(myobj,'System.Double')
! doublevar=myDouble
write(,100) fldname,otype, doublevar
case ('System.String')
charvar=tostring(myobj)
! charvar=char(cast@(myobj,'System.String'))
write(,100) fldname,otype, charvar
end select
100 Format (A20,2X, A15,2X, G35.0)
end do
end do
read * call dc_close(myConn)
end program
Supporting C# DLL:
using System; using System.Collections.Generic; using System.Text;
namespace ItemSelector { static public class ItemSelector { static public System.Data.DataTable GetTable(System.Data.DataTableCollection MyCollection, int Item) { return(MyCollection[Item]); } static public System.Data.DataColumn GetColumn(System.Data.DataColumnCollection MyCollection, int Item) { return ((System.Data.DataColumn)MyCollection[Item]); } static public System.Data.DataRow GetRow(System.Data.DataRowCollection MyCollection, int Item) { return (MyCollection[Item]); } static public Int32 ObjectToLong(object myobject) { if (myobject is Int32) return (int)myobject; else throw new InvalidCastException(); } static public Int16 ObjectToShort(object myobject) { if (myobject is Int16) return (Int16)myobject; else throw new InvalidCastException(); } static public Single ObjectToSingle(object myobject) { if (myobject is Single) return (Single)myobject; else throw new InvalidCastException(); } static public double ObjectToDouble(object myobject) { if (myobject is Double) return (Double)myobject; else throw new InvalidCastException(); } static public string ObjectToString(object myobject) { if (myobject is String) return (string)myobject; else throw new InvalidCastException(); }
}
}