After the configuration of the Hive ODBC driver is successful, it becomes easier to access it through C #, which is divided into query and update operations, directly attached to the test code. The target platform for C # Engineering compilation needs to be noted in this process
- Read-Write access code example:
Public classhiveodbcclient {/// <summary> /// /// </summary> Public Statichiveodbcclient Current {Get{return Newhiveodbcclient ();} } /// <summary> /// /// </summary> /// <param name= "context" ></param> Public voidExcutenoquery (stringDnsstringSQL) {OdbcConnection conn=NULL; Try{conn=Newodbcconnection (DNS); Conn. Open (); OdbcCommand cmd=NewOdbcCommand (SQL, conn); Cmd. ExecuteNonQuery (); } Catch(Exception ex) {Throwex; } finally { if(NULL!=conn) {Conn. Close (); } } } /// <summary> /// /// </summary> /// <param name= "context" ></param> /// <returns></returns> PublicDataSet Query (stringDnsstringSqlstringTblname ="TBL") {DataSetSet=NewDataSet (); OdbcConnection Conn=NULL; Try{conn=Newodbcconnection (DNS); Conn. Open (); OdbcCommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; OdbcDataAdapter Adapter=Newodbcdataadapter (CMD); Adapter. Fill (Set, tblname); } Catch(Exception ex) {Throwex; } finally { if(NULL!=conn) {Conn. Close (); } } return Set; } }
View Code
- Test: Create a new console project, test table creation and data insertion, as shown in the sample code:
classProgram {Static voidMain (string[] args) {Console.WriteLine ("Press any key to start the build table and data insertion test"); Console.readkey (); stringDNS ="dsn=myhive; uid=hive; Pwd="; stringsql ="CREATE TABLE Employee (ID string,code string,name string)"; HiveOdbcClient.Current.ExcuteNoQuery (Dns,sql); SQL="INSERT INTO Table Employee values (' 002 ', ' 002 ', ' Zhangshan ');"; HiveOdbcClient.Current.ExcuteNoQuery (DNS, SQL); Console.WriteLine ("Data Insert complete, press any key to exit"); Console.readkey (); } }
- Using the Squrriel tool described in the previous chapter, execute: Select * from employee, display data inserted in code OK
Hive (iv): C # accesses hive through ODBC