1、/// <summary>

/// 获取当前目标表结构

/// </summary>

/// <param name="tableName">目标表</param>

/// <returns></returns>

public static DataTable GetOracleTableSchema(string tableName)

{             BS.EAP.DBAccess.IDataBase oDB = BS.EAP.DBAccess.DBFactory.GetDBInstance();

string sql = string.Format(@"             select t.COLUMN_NAME as name, t.DATA_LENGTH as ColLength,t.DATA_TYPE as Typeid, t.DATA_SCALE as Scale,decode(nvl(tp.column_name, ''),'',0,1) as isPrimary,                    '{0}' as tableName, '{0}' as viewName, 0 as viewField,                    (case t.NULLABLE when 'Y' then 1 else 0 end) as isNullable, tp.column_name             from USER_TAB_COLS t             left join (select col.table_name, col.column_name                  from user_constraints con,  user_cons_columns col                  where con.constraint_name = col.constraint_name and con.constraint_type='P'                  ) tp on tp.table_name = t.TABLE_NAME and tp.column_name = t.column_name             where  t.HIDDEN_COLUMN ='NO' and t.TABLE_NAME = '{0}'", tableName.ToUpper());

return oDB.GetDataTable(sql);

}

2、/// <summary>
        /// 生成批量插入Sql语句
        /// </summary>
        /// <param name="DestinationTableName">目标表</param>
        /// <param name="table">数据源</param>
        /// <param name="cmd">操作命令</param>
        /// <param name="DicInitData">初始数据</param>
        /// <returns></returns>
        public static string GenerateInserSql(string DestinationTableName, DataTable table, string strDsid)
        {
            BS.EAP.DBAccess.IDataBase oDB = DBFactory.GetDBInstance();
            DataTable dt = new DataTable();
            string column_name = string.Empty;
            string param = string.Empty;
            //查询表机构字段
            dt = oDB.GetDataTable(@"select column_name EnKey from 
                                        user_tab_columns   where  lower(table_name)=   '" + DestinationTableName.ToLower() + "' order by COLUMN_ID");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                column_name += dt.Rows[i][0].ToString() + ",";
                param += ":" + dt.Rows[i][0].ToString() + ",";
            }
            column_name = column_name.Substring(0, column_name.Length - 1);
            param = param.Substring(0, param.Length - 1);
            return string.Format("insert into " + DestinationTableName + "(" + column_name + ") values(" + param + ")");
        }

3、/// <summary>
        /// 构造参数
        /// </summary>
        /// <param name="DestDataTable"></param>
        /// <param name="table"></param>
        /// <param name="cmd"></param>
        public static void GenerateParameter(DataTable DestDataTable, DataTable table, OracleCommand cmd, string Dsid, string TableName, DataTable dbwell)
        {
            string strTypID = string.Empty;
            string strColumName = string.Empty;
            string IsRep = string.Empty;
            int colCount = table.Columns.Count;
            for (int i = 0; i < colCount; i++)
            {
                    strColumName = table.Columns[i].ColumnName;
                    OracleParameter parameter = new OracleParameter();
                    parameter.ParameterName = strColumName;
                    parameter.Direction = ParameterDirection.Input;
                    int iScale = 0;
                    strTypID = GetColumnType(strColumName, DestDataTable, out iScale);
                    DbType oDbType = GetDataFieldType(strTypID, iScale);
                    parameter.DbType = oDbType;
                     parameter.Value = GetObjectArray(table, strColumName, oDbType, dbwell);
                    cmd.Parameters.Add(parameter);
            }
        }

4、GetColumnType方法

/// <summary>

/// 获取字段类型         /// </summary>

/// <param name="columnName">字段名称</param>

/// <param name="dt">数据表</param>

/// <returns></returns>

public static string GetColumnType(string columnName, DataTable dt, out int iScale)

{

string strType = string.Empty;

iScale = 0;

for (int iRow = 0; iRow < dt.Rows.Count; iRow++)

{

string strColName = dt.Rows[iRow]["Name"].ToString();

if (strColName.ToUpper() == columnName.ToUpper())

{

strType = dt.Rows[iRow]["TypeID"].ToString();

if (dt.Rows[iRow]["SCALE"].ToString() == "" || dt.Rows[iRow]["SCALE"].ToString() == "0")

iScale = 0;

else

iScale = Convert.ToInt32(dt.Rows[iRow]["SCALE"].ToString());

break;

}

}

return strType;

}

5、// <summary>
        /// 获取字段类型
        /// </summary>
        /// <param name="strType">字段类型</param>
        /// <param name="iScale">小数位数</param>
        /// <returns></returns>
        public static DbType GetDataFieldType(string strType, int iScale)
        {
            DbType oDbType = DbType.String;
            switch (strType.ToUpper())
            {
                case "VARCHAR2":
                    oDbType = DbType.String;
                    break;
                case "DATE":
                    oDbType = DbType.DateTime;
                    break;
                case "NVARCHAR2":
                    oDbType = DbType.String;
                    break;
                case "NUMBER":
                    if (iScale == 0)
                        oDbType = DbType.Int32;
                    else
                        oDbType = DbType.Decimal;
                    break;
                case "FLOAT":
                    oDbType = DbType.Decimal;
                    break;
                case "LONG":
                    oDbType = DbType.Decimal;
                    break;
                case "BINARY_FLOAT":
                    oDbType = DbType.Decimal;
                    break;
                case "BINARY_DOUBLE":
                    oDbType = DbType.Decimal;
                    break;
                default:
                    break;
            }
            return oDbType;
        }

6、/// <summary>
        ///  构造值数组,供赋值使用
        /// </summary>
        /// <param name="dt">数据源表</param>
        /// <param name="colname">列名</param>
        /// <returns></returns>
        public static object[] GetObjectArray(DataTable dt, string colname, DbType oDbType, DataTable dbwell)
        {
            object[] objArray = new object[dt.Rows.Count];
            int iCol = 0;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                if (dt.Columns[i].ColumnName == colname)
                {
                    iCol = i;
                    break;
                }
            }
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                    if (string.IsNullOrEmpty(dt.Rows[j][iCol].ToString()))
                        objArray[j] = DBNull.Value;
                    else
                        objArray[j] = GetObjValue(dt.Rows[j][iCol], oDbType);
            }
            return objArray;
        }

.net 调用Oracle.Data.Access 组件提供的用于批量操作的方法—获取数据库表结构方法和跟参数赋值方法的更多相关文章

  1. .net 调用Oracle.Data.Access 组件提供的用于批量操作的方法

    1.添加引用 using Oracle.DataAccess.Client; using System.Configuration; 2.代码 增加方法 //DestinationTableName ...

  2. Windows 的 Oracle Data Access Components (ODAC)

     下载 x64bit https://www.oracle.com/technetwork/cn/database/windows/downloads/index.html 适用于 Windows 的 ...

  3. MySQL,SQLSERVER,ORACLE获取数据库表名及字段名

    1.MySQL 获取表名: 用“show tables”命令.在程序中也可以采用该命令获取,在返回的RowSet中的“Tables_in_db”读出来.其中“db”是指你的数据库的名称,比如说Tabl ...

  4. ORACLE 中写入txt文本与从Txt文件中读入数据 修改表结构

    --创建一个表 DROP TABLE TEST CASCADE CONSTRAINTS ; CREATE TABLE TEST(A VARCHAR(30),B VARCHAR(30)); --查看具体 ...

  5. oracle数据库自动生成数据库表结构文档(亲测有效)

    import java.awt.Color; import java.io.FileOutputStream; import java.sql.Connection; import java.sql. ...

  6. 使用 Oracle Data Access Components连接oracel

    使用微软自带的oracle连接类,在framework4.0中被标识为弃用,强行用它开发了Winform程序,发布放到XP上提示: Error System.Data.OracleClient req ...

  7. (转)oracle使用expdp、impdp和exp、imp导入导出表及表结构

    使用expdp.impdp和exp.imp时应该注重的事项: 1.exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. 2.expdp和impdp是服务端的工具程序,他们只能在 ...

  8. Oracle数据库表结构导出

    1. 在PL/SQL中找到"工具--导出用户对象"菜单.点击运行.

  9. oracle 使用sql获取数据库表,表的字段

    --第一种方法: 查询dba_tab_columns select COLUMN_NAME,DATA_TYPE,DATA_LENGTH  from   dba_tab_columns where  t ...

随机推荐

  1. sqlite--代码操作

    1.创建数据库 NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...

  2. 爆炸!iOS资源大礼包(持续更新...)

    今天为大家整理了一些关于iOS学习的干货,献给正在奋斗的你们,首先声明一下,在整理的过程中参考了大量的博客和文章,知识的分享终究会增值,在此表示感谢,希望这篇文章给大家带来帮助. 基础部分: C语言教 ...

  3. Implement Insert and Delete of Tri-nay Tree

    问题 Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but wi ...

  4. css3之background

    background background: (1)url(image1.png) right bottom, (2)url(image2.png) center, (3)url(image3.png ...

  5. [Mugeda HTML5技术教程之7]添加动画

    前一节我们讲述了怎么在新建的作品中添加元素,元素加好以后我们还想让他们动起来,来实现比较炫的效果.这节我们将要讲述怎么给元素添加动画.Mugeda动画是通过时间轴和帧来实现的.通过在时间轴上创建图层和 ...

  6. php 之 类,对象

    --恢复内容结束--- 一.类和对象: 1.定义: 对象:我们所见到的东西都可以称之为对象,是类实例化出来的东西 类:是对所有的同类对象抽象出来的东西 eg: 在一张表中记录了全班同学的学号,姓名,性 ...

  7. Clover周报模块 -- 开发总结

    2014年7月8日 00:16:05 一.切图 这次开发,切图花了不少时间,样式是用scss写的,第一次用,不过用着用着就发现它的强大,层级.作用域.重用等都非常的方便,还有考拉神器,用着真是爽!不过 ...

  8. php 中_set()_get()实例解析

    <?php class Person { // 下面是人的成员属性, 都是封装的私有成员 private $name; // 人的名子 private $sex; // 人的性别 private ...

  9. HTML&CSS基础学习笔记1.17-表格的头部与尾部

    表格的头部和尾部 既然有标签表示表格的主体,那么自然表格的头部和尾部也有对应的标签. HTML中使用<thead>标签表示表格的头部,使用<tfoot>标签表示表格的尾部. 有 ...

  10. C程序设计语言练习题1-8

    练习1-8 编写一个统计空格.制表符与换行符个数的程序. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // 定义名为main的函数,它 ...