| WideStudio Programming Guide | WideStudio Index Table of contents

> ## 利用数据库Class的数据库访问

> ### 通过ODBC进行数据库访问

> 可以利用WSCvdb Class，通过ODBC接口进行数据库访问。
> 使用ODBC访问时，属性 WSNtype 指定为 WS_DB_ODBC，在WSCvdb::open 函数中指定DSN 、用户注册名，密码进行实行。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       long ret = newvdb__000->open("dn","user","passwd");
>       if (ret == WS_NO_ERR){
>         //Connecting
>       }else{
>         //Connection failed with getting an error message.
>         char buffer[1024];
>         newvdb__000->getErrorMsg(buffer,1024);
>       }
>     }

> 通过ODBC进行访问时，在WSNhostname中设定DSN,在WSNusername中设定用户名，在WSNpassword中设定密码，不带参数实行WSCvdb::open函数。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       long ret = newvdb__000->open();
>       if (ret == WS_NO_ERR){
>         //Connecting.
>       }else{
>         //Connection failure with getting an error message.
>         char buffer[1024];
>         newvdb__000->getErrorMsg(buffer,1024);
>       }
>     }

> ### 进行PostgreSQL接口的数据库访问

> 通过利用WSCvdb Class，可以通过 PostgreSQL 接口直接PostgreSQL 数据库访问。
> 使用PostgreSQL 接口进行访问时，属性WSNtype 指定为 WS_DB_POSTGRES ，在WSCvdb::open 函数中指定主机名，用户注册名，密码，数据库名，端口号码进行实行。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       long ret = newvdb__000->open("dn","user","passwd","dbname","5432");
>       if (ret == WS_NO_ERR){
>         //Connecting.
>       }else{
>         //Connection failure with getting an error message.
>         char buffer[1024];
>         newvdb__000->getErrorMsg(buffer,1024);
>       }
>     }

> 通过PostgreSQL进行访问时，在WSNhostname中设定主机名,在WSNusername中设定用户名，在WSNpassword中设定密码，WSNdbname中设定数据库名，在WSNport中指定端口号码，不带参数实行WSCvdb::open函数。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       long ret = newvdb__000->open();
>       if (ret == WS_NO_ERR){
>         //Connecting.
>       }else{
>         //Connection failure with getting an error message.
>         char buffer[1024];
>         newvdb__000->getErrorMsg(buffer,1024);
>       }
>     }

> ### 表数据的作成

> 实行WSCvdb::open 对数据库连接成功后，能发行SQL来操作数据库。下面的例子中，是数据库上制作一个shinamono 表数据的例子。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       char buf1[512];
>       strcpy(buf1,
>              "create table shinamono(code int, hinmei char(20), nedan float)");
>       newvdb__000->sqlExecute(buf1);

>       if (ret == WS_NO_ERR){
>         //Success
>       }else{
>         //Connection failure with getting an error message
>         char buffer[1024];
>         newvdb__000->getErrorMsg(buffer,1024);
>       }
>     }

> ### 表数据中追加数据

> 实行WSCvdb::open 对数据库的连接成功后，如果有可以操作的表数据，则可发行SQL将数据追加到表数据。下面的例子，是在数据库上名为shinamono的表数据里追加数据的例子。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       newvdb__000->beginTran();
>       strcpy(buf1, "insert into shinamono values(1, 'Orange', 100)");
>       newvdb__000->sqlExecute(buf1);
>       strcpy(buf1, "insert into shinamono values(2, 'Apple', 200)");
>       newvdb__000->sqlExecute(buf1);
>       strcpy(buf1, "insert into shinamono values(3, 'Banana', 300)");
>       newvdb__000->sqlExecute(buf1);
>       strcpy(buf1, "insert into shinamono values(4, 'Melon', 0)");
>       newvdb__000->sqlExecute(buf1);

>       newvdb__000->commitTran();

>     }

> ### 参照表数据上的数据

> 实行WSCvdb::open 对数据库的连接成功后，如果存在可以参照的表数据，则可发行SQL来参照表数据上的数据。下面的例子，是从数据库上名为 shinamono 的表数据中参照数据，并且更新数据。

>     #include <WScom.h>
>     #include <WSCfunctionList.h>
>     #include <WSCbase.h>
>     //----------------------------------------------------------
>     //Function for the event procedure
>     //----------------------------------------------------------
>     #include <WSCvdb.h>
>     #include <WSCdb.h>
>     extern WSCvdb* newvdb__000;

>     void db_ep(WSCbase* object){
>       WSCvariant var;
>       WSCstring outp;
>       newvdb__000->beginTran();
>       WSCdbRecord rs(newvdb__000);
>       if(rs.open("select * from shinamono order by code") == WS_NO_ERR) {
>         while (!rs.isEOF()) {
>           outp = "";
>           rs.getColValue("code", &var);
>           int code = (int)var;
>           outp << "code:" << (int)var << " ";
>           rs.getColValue("hinmei", &var);
>           outp << "hinmei:" << (char*)var << " ";
>           rs.getColValue("nedan", &var);
>           char buf[80];
>           char buf1[80];
>           double nedan = (float)var + 10;

>           sprintf(buf, "%f", (float)var);
>           outp << "nedan:" << buf << "\n";
>           printf((char*)outp);

>           if(nedan != 0) {
>             sprintf(buf1, "update shinamono set nedan = %f where code = %d",
>                     nedan, code);
>           } else {
>             sprintf(buf1, "delete from shinamono where code = %d", code);
>           }
>           newvdb__000->sqlExecute(buf1);
>           rs.moveNext();
>         }
>       }
>       rs.close();
>       newvdb__000->commitTran();
>     }

Document Release 3.70 for WideStudio ver 3.70, May 2004

WideStudio documents index | Table of contents

Copyright(C) WideStudio Development Team, 1999-2004 | Last modified: May 25, 2004