• C# + Oracel 批量插入数据(List,Array等)


    1. static OracleConnection OpenConn()
    2. {
    3. string connstr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=***.***.***.***)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=***)));Persist Security Info=True;User ID=***;Password=***;";
    4. OracleConnection conn = new OracleConnection();
    5. conn.ConnectionString = connstr;
    6. conn.Open();
    7. return conn;
    8. }
    9. public bool insert(string tableName, IList list, int length)
    10. {
    11. string sql = "INSERT INTO " + tableName + "(XH,MMSI,UTC,LAT,LON,SOG,COG,UDATE) values(SEQ_SHIPPOINT.Nextval,:MMSI,:UTC,:LAT,:LON,:SOG,:COG,:UDATE)";
    12. //string sql = "INSERT INTO " + tableName + "({0}) values(SEQ_SHIPPOINT.Nextval,{1})";
    13. using (OracleConnection conn = OpenConn())
    14. {
    15. try
    16. {
    17. int returnrow = 0;
    18. int recordCount = list.Count;
    19. using (OracleCommand cmd = conn.CreateCommand())
    20. {
    21. cmd.CommandText = sql;
    22. cmd.ArrayBindCount = recordCount; //指定单次需要处理的条数
    23. //注意,这里的值是数组
    24. cmd.Parameters.Add(new OracleParameter(":MMSI", OracleDbType.Varchar2, ParameterDirection.Input)).Value = list.Select(x => x.mmsi).ToArray(); //item.Name;
    25. cmd.Parameters.Add(new OracleParameter(":UTC", OracleDbType.Long, ParameterDirection.Input)).Value = list.Select(x => x.utc).ToArray();
    26. cmd.Parameters.Add(new OracleParameter(":LAT", OracleDbType.Long, ParameterDirection.Input)).Value = list.Select(x => x.lat).ToArray();
    27. cmd.Parameters.Add(new OracleParameter(":LON", OracleDbType.Long, ParameterDirection.Input)).Value = list.Select(x => x.lon).ToArray();
    28. cmd.Parameters.Add(new OracleParameter(":SOG", OracleDbType.Long, ParameterDirection.Input)).Value = list.Select(x => x.sog).ToArray();
    29. cmd.Parameters.Add(new OracleParameter(":COG", OracleDbType.Long, ParameterDirection.Input)).Value = list.Select(x => x.cog).ToArray();
    30. cmd.Parameters.Add(new OracleParameter(":UDATE", OracleDbType.Date, ParameterDirection.Input)).Value = list.Select(x => x.udate).ToArray();
    31. returnrow = cmd.ExecuteNonQuery();
    32. }
    33. return returnrow > 0;
    34. }
    35. catch (Exception ex)
    36. {
    37. //LogHelper.WriteErrLog("insertOracle", ex.Message);
    38. return false;
    39. }
    40. }
    41. }

  • 相关阅读:
    密码学系列之:PEM和PKCS7,PKCS8,PKCS12
    【数据结构与算法】排序(下篇)
    SY6288AAAC低耗配电开关USB限流开关600MA
    测试员如何面对30岁后的下坡路,伤不起的年龄,职业道路何去何从?
    停车场如何通过金万维快解析实现数据共享
    十二、排序
    浮点数的加减法运算
    js回车事件的使用
    向后量子密码学迁移!美国NIST公布12家合作伙伴
    Spring Boot事件监听机制:原理、实践与优化之道
  • 原文地址:https://blog.csdn.net/ttbat/article/details/126711526