ManagedDataAccess连数据库
Posted 富坚老贼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ManagedDataAccess连数据库相关的知识,希望对你有一定的参考价值。
Oracle官方的托管数据库访问组件,单DLL,Oracle.ManagedDataAccess.dll,不安装Oracle客户端,不区分32/64位, 直接引用即可。
与非托管程序为Oracle.DataAccess.dll相比,不支持BulkCopy和CustomType等。
下载
https://www.oracle.com/database/technologies/odac-downloads.html
或者
nuget
文档
https://docs.oracle.com/cd/E11882_01/win.112/e23174/toc.htm
类介绍
OracleCommand
OracleCommandBuilder
OracleConnection
OracleDataAdapter
OracleDatabase
OracleDataReader
OracleError
OracleErrorCollection
OracleException
OracleInfoMessageEventArgs
OracleParameter
OracleParameterCollection
OraclePermission
OraclePermissionAttribute
OracleRowUpdatedEventArgs
OracleRowUpdatingEventArgs
OracleTransaction
DataSet接收
DataSet ds = new DataSet(); string str = @"Data Source=xxx:1521/orcl; User Id=xxx; password=xxx;Pooling=false"; string sql = "select * from T_MD_SCALE"; try { using (OracleConnection conn = new OracleConnection(str)) { conn.Open(); if (conn.State == ConnectionState.Open) { OracleDataAdapter da = new OracleDataAdapter(sql, conn); da.Fill(ds, "ds"); } //测试 Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine(ex.Message); }
获得某个表的列名和类型
static void Main(string[] args) { DataSet ds = new DataSet(); string str = @"Data Source=xxx:1521/orcl; User Id=xxx; password=xxx;Pooling=false"; string sql = "select * from T_MD_SCALE"; try { using (OracleConnection conn = new OracleConnection(str)) { conn.Open(); if (conn.State == ConnectionState.Open) { OracleCommand cmd = new OracleCommand(sql, conn); OracleDataReader reader = cmd.ExecuteReader(); for (int i = 0; i < reader.FieldCount; i++) { // Console.WriteLine("列名是:"+reader.GetName(i)); Console.WriteLine("该列类型:" + reader.GetFieldType(i)); Console.WriteLine("-----------------------------"); } } //测试 Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
获取表的架构
static void Main(string[] args) { DataSet ds = new DataSet(); string str = @"Data Source=192.166.208.52:1521/orcl; User Id=IMES_PMS1; password=pms1;Pooling=false"; string sql = "select * from T_MD_SCALE"; try { using (OracleConnection conn = new OracleConnection(str)) { conn.Open(); if (conn.State == ConnectionState.Open) { OracleCommand cmd = new OracleCommand(sql, conn); OracleDataReader reader = cmd.ExecuteReader(); //架构 DataTable dt = reader.GetSchemaTable(); } //测试 Console.ReadKey(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
配合上查询数据库的所有表名
select * from tab
就可以自定义生成model和dal了
以上是关于ManagedDataAccess连数据库的主要内容,如果未能解决你的问题,请参考以下文章
Oracle.ManagedDataAccess:TNS:无法解析指定的连接标识符
Oracle.ManagedDataAccess 和 ORA-01017:用户名/密码无效;登录被拒绝
C#连接Oracle的时候,Oracle.DataAccess.dll能连接数据库,Oracle.ManagedDataAccess却不能?
C#工具类OracleHelper,基于Oracle.ManagedDataAccess.Client封装