数据库 oracle 函数

复制代码

static OracleConnection mQracleConnecting = null; public static OracleConnection QracleConnecting { get { try { if(mQracleConnecting ==null) mQracleConnecting = new OracleConnection(jksg); if (mQracleConnecting.State != ConnectionState.Open) mQracleConnecting.Open(); return mQracleConnecting; } catch (Exception) { return null; } } } public static DataTable DBGetDataTable(string sql,string conn) { DataTable dataSet = new DataTable(); OracleDataAdapter OraDA = new OracleDataAdapter(sql, conn); OraDA.Fill(dataSet); return dataSet; } public static DataTable DBGetDataTable(string sql, OracleConnection conn) { DataTable dataSet = new DataTable(); OracleDataAdapter OraDA = new OracleDataAdapter(sql, conn); OraDA.Fill(dataSet); return dataSet; } // 执行SQL语句,返回所影响的行数 public static int ExecuteSQL(string sql) { int Cmd = 0; OracleCommand command = new OracleCommand(sql, QracleConnecting); try { Cmd = command.ExecuteNonQuery(); } catch { } return Cmd; } public static int ExecuteSQL_Update(string sql) { OracleDataAdapter OraDA = new OracleDataAdapter(sql, QracleConnecting); return OraDA.UpdateCommand.ExecuteNonQuery(); } public static int ExecuteSQL_Insert(string sql) { OracleDataAdapter OraDA = new OracleDataAdapter(sql, QracleConnecting); return OraDA.InsertCommand.ExecuteNonQuery(); }
复制代码

 

2.存储obj,这里默认的为 EF 框架出来的类相似,表名为类名,字段是列名

复制代码
public string GetInsertstring(object item)
        {
            string sql = "INSERT INTO {2} ({0}) VALUES ({1})";

            var members = item.GetType().GetProperties();
            var classname = item.GetType().Name;
            string mems="";
            string values="";
            foreach(var me in members)
            {
                mems += me.Name +",";
                var val =me.GetValue(item);

                if (me.PropertyType == typeof(System.DateTime))
                {
                    if (val == null)
                    {
                        values += ",";
                        continue;
                    }
                    System.DateTime time = (System.DateTime)val;
                    values += "TO_DATE('" + time.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-mm-dd hh24:mi:ss'),";
                }
                else if (me.PropertyType == typeof(string))
                {
                    if (val == null)
                    {
                        values += "'',";
                        continue;
                    }
                    values += "'" +val.ToString()+"',";
                }
                else
                {
                    if (val == null)
                    {
                        values += ",";
                        continue;
                    }
                    values += val.ToString() + ",";
                }
            }
            mems= mems.Substring(0,mems.Length-1);
            values = values.Substring(0, values.Length - 1);
            sql= string.Format(sql, mems, values,classname);
            return sql;
        }
复制代码

 

posted @   leolzi  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示