Hive (iv): C # accesses hive through ODBC

Source: Internet
Author: User
Tags odbc

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.