f# - F sharp saving record type into access db -
i have following record type "tbl" , contain lists or seq. how can save/update "tbl" existing access db table using f sharp field names specified below.
type tbl= { p:string; in:system.datetime; ex:system.datetime; b:string; cc:string; gg:double; pr:double; de:double; pre:double; deu:double; prpl_deduc:double; gur:double; gc:double; prp:double; pda:double; pro:double} let conn = new oledbconnection( @"provider=microsoft.ace.oledb.12.0; data source=t:\test.accdb; persist security info=false;" ) conn.open() let sql="insert test select * " tbl let dadapter = new oledbdataadapter(sql,conn) dadapter.update
there's couple of things come mind here.
firstly, think you're tripping on interpretation of record type - isn't recordset - can't pump oledbdataadapter.
there's not support access in f# type providers - i'm losing touch how many there example sqlprovider lets read access not perform crud actions.
so, @ least far know, old fashioned oledb approach you've got above right path here.
the code below uses sqlprovider retrieve contents of access db 3 columns, savearow function won't work , goes on oledb way...
#r @"packages\sqlprovider.0.0.9-alpha\lib\net40\fsharp.data.sqlprovider.dll" open system open system.linq open fsharp.data.sql [<literal>] let cnstr = @"provider=microsoft.ace.oledb.12.0;data source=c:\temp\database1.accdb;persist security info=false;" type sql = sqldataprovider< connectionstring = @"provider=microsoft.ace.oledb.12.0;data source=c:\temp\database1.accdb;persist security info=false;", databasevendor = common.databaseprovidertypes.msaccess> let ctx = sql.getdatacontext() // return data ctx.``[database1].[table1]`` |> seq.take 1 // create type represent data type row = {p: string; in:system.datetime; gg:double} // insert more data p:string, in:datetime, ex:datetime, b:string, gg:number // unfortunately sqldataprovider doesn't support crud operations on ms access... let savearow (arow: row) = let table1 = ctx.``[database1].[table1]``.create() table1.p <- arow.p table1.in <- arow.in table1.gg <- arow.gg ctx.submitupdates() {p="abc"; in=datetime.now; gg=200.} |> savearow // try old fashioned way open system.data open system.data.oledb #r @"c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.5.2\system.transactions.dll" open system.transactions let savearow2(arow: row) = let cn = new oledbconnection(cnstr) cn.open() let cmd = new oledbcommand() cmd.commandtype <- commandtype.text let insertcmd = sprintf "insert table1 values ('%s', '%s', '%f')" arow.p (arow.in.toshortdatestring()) arow.gg cmd.commandtext <- insertcmd cmd.connection <- cn cmd.executenonquery() |>ignore cn.close() {p="abcd"; in=datetime.now; gg=200.} |> savearow2
Comments
Post a Comment