你的位置:首页 > 数据库

[数据库]CUBRID学习笔记 3 net连接数据库并使用


接上文

数据库安装好后,也可以测试语句了.

下面我们用c#写一个控制台程序,连接数据库,并读取数据.

一 下载驱动  net版的下 CUBRID ADO.NET Data Provider 9.3.0.0001.zip 这个就可以.解压后里面有一个CUBRID.Data.dll

二 创建控制台,添加对CUBRID.Data.dll的引用

三 写代码

如下

using System;using System.Collections.Generic;using System.Linq;using System.Text;using CUBRID.Data.CUBRIDClient;namespace cubridtest {class Program {  static void Main(string[] args)  {    CUBRID.Data.CUBRIDClient.CUBRIDConnectionStringBuilder sb = new CUBRIDConnectionStringBuilder();    sb.User = "public";    sb.Database = "demodb";    sb.Port = "33000";    sb.Server = "192.168.2.156";    sb.Password = "";    using (CUBRIDConnection conn = new CUBRIDConnection(sb.GetConnectionString()))    {      conn.Open();      conn.SetAutoCommit(false);      using (CUBRIDCommand cmd = new CUBRIDCommand("select * from athlete limit 1,10", conn))      {        using (System.Data.Common.DbDataReader reader = cmd.ExecuteReader())        {          while (reader.Read())          {            Console.WriteLine( reader[0].ToString()+";");          }        }      }    }    Console.ReadKey();  }}}

  结果如下:

 

 

其中 

SetAutoCommit的说明如下
CCI_DEFAULT_AUTOCOMMIT

CCI_DEFAULT_AUTOCOMMIT is a parameter used to configure whether to make application implemented in CCI interface or CCI-based interface such as PHP, ODBC, OLE DB, Perl, Python, and Ruby commit automatically. The default value is ON. This parameter does not affect applications implemented in JDBC. In case of using ODBC, malfunction can occur if this parameter is ON; you must set it to OFF, in this case.

If the CCI_DEFAULT_AUTOCOMMIT parameter value is OFF, the broker application server (CAS) process is occupied until the transaction is terminated. Therefore, it is recommended to execute commit after completing fetch when executing the SELECTstatement.

 

Note The CCI_DEFAULT_AUTOCOMMIT parameter has been supported from 2008 R4.0, and the default value is OFF for the version. Therefore, if you use CUBRID 2008 R4.1 or later versions and want to keep the configuration OFF, you should manually change it to OFF to avoid auto-commit of unexpected transaction.

更详细的文档 http://www.cubrid.org/wiki_apis/entry/ado-net-driver-development-notes  最新版是9.3了,这个文章是8的.

如果感兴趣可以看源码吧