论坛首页 综合技术论坛

Erlang To ADO.NET

浏览 2835 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-01-23  

Erlang访问关系数据库的方式极为有限,目前似乎只有ODBC。使用ODBC时有时候会莫名其妙地出现一些错误,很难查找原因。故写了一个ADO.NET的数据访问模块,以期在项目中使用,感觉还不错。

现介绍一下基本使用情况,使用前必须设置一下环境变量set ARCHDIR=windows,然后:

application:start(adonet)

 

然后,连接数据库:

    {ok, Ref} = adonet:open("User ID=sa;Data Source=192.168.1.133;Password=soft123456;Initial Catalog=trade;Provider=SQLOLEDB.1;"),

 

查询数据:

adonet:select(Ref,"select name,id,code from goods where id>?",[3])

 

成功执行将返回:

{ok,["name","id","code"],
    [{"ddd",4,"332"},
     {"微波炉",5,"004"},
     {[],6,"005"},
     {"er",7,"006"},
     {"液晶电视",8,"098"},
     {"洗衣机",9,"007"},
     {"TCL电脑",10,"008"},
     {"名称\nabc",11,"001"}]}

 

 

插入,删除待操作可以使用do,例如:

adonet:do(Ref, "insert into goods(name,code) values(?,?)",
["name1","001"])

 

目前仅实现select和do二个函数,其他的以后慢慢再完成。

 

   发表时间:2008-01-24  
更新了select数据的实现方法,原先用eval,现在改成binary_to_term了,这样速度更快。
0 请登录后投票
   发表时间:2008-01-30  

增加了对事务的支持:

1、开始事务

{ok} = adonet:begin_transaction(Ref).

 

2、提交事务
{ok} = adonet:commit(Ref).
 3、回滚事务
{ok} = adonet:rollback(Ref)
 基本用法代码如下:
start() ->
    application:start(adonet),
    {ok, Ref} = adonet:open("User ID=sa;Data Source=192.168.1.133;Password=soft123456;Initial Catalog=trade;Provider=SQLOLEDB.1;"),
    {ok} = adonet:begin_transaction(Ref),
    case catch(do_something(Ref)) of
        {'EXIT', Reason} ->
            adonet:rollback(Ref),
            io:format("exit:~p~n", [Reason]);
        ok ->
            adonet:commit(Ref),
            io:format("ok\n")
    end.
    
do_something(Ref) ->
    {ok,_} = adonet:do(Ref, "insert into goods(name) values(?)", ["name02"]),
    {ok,_} = adonet:do(Ref, "insert into goods(name) values(?)", ["name03"]),
    ok.
 
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics