hive odbc 驅動配置成功後,通過c#訪問就變得比較簡單了,分為查詢與更新操作,直接附上測試代碼。在此過程中需要註意c#工程編譯的目標平臺 讀寫訪問代碼示例: public class HiveOdbcClient { /// <summary> /// /// </summary> publ ...
hive odbc 驅動配置成功後,通過c#訪問就變得比較簡單了,分為查詢與更新操作,直接附上測試代碼。在此過程中需要註意c#工程編譯的目標平臺
- 讀寫訪問代碼示例:
public class HiveOdbcClient { /// <summary> /// /// </summary> public static HiveOdbcClient Current { get { return new HiveOdbcClient(); } } /// <summary> /// /// </summary> /// <param name="context"></param> public void ExcuteNoQuery(string dns, string sql) { OdbcConnection conn = null; try { conn = new OdbcConnection(dns); conn.Open(); OdbcCommand cmd = new OdbcCommand(sql, conn); cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { if (null != conn) { conn.Close(); } } } /// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> public DataSet Query(string dns, string sql,string tblName = "tbl") { DataSet set = new DataSet(); OdbcConnection conn = null; try { conn = new OdbcConnection(dns); conn.Open(); OdbcCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; OdbcDataAdapter adapter = new OdbcDataAdapter(cmd); adapter.Fill(set, tblName); } catch (Exception ex) { throw ex; } finally { if (null != conn) { conn.Close(); } } return set; } }
View Code - 測試:新建一個控制台工程,測試表創建及數據插入,示例代碼如下:
class Program { static void Main(string[] args) { Console.WriteLine("按任意鍵開始進行建表及數據插入測試"); Console.ReadKey(); string dns = "DSN=myhive;UID=hive;PWD="; string sql = "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("數據插入完成,按任意鍵退出"); Console.ReadKey(); } }
- 使用上一章介紹的SQurriel 工具,執行: Select * from employee ,顯示代碼中插入的數據則OK