Home  >  Article  >  Database  >  asp.net Oracle 之Insert 与 Update 更新封闭代码

asp.net Oracle 之Insert 与 Update 更新封闭代码

WBOY
WBOYOriginal
2016-06-07 17:45:48955browse

.net oracle 之insert 与 update 更新封闭代码
 
觉得我可以从的更新方法下手,于是有了下面这些尝试了。

在oracle 中insert 语句是可以使用 returning 返回新增的记录的。

于是我们的insert 语句就是这样了.

insert into tablename (uniquecolumn,othercolumns)
values(table_seq.nextval,values) returning uniquecolumn into :unique_id。

这样执行之后,我就可以通过out参数 unique_id 获取新增的记录的主键啦

 

public override object insert(editorparams pm)
        {
            if (pm != null && pm.editvalues.count > 0)
            {
                querysetting qs = configenginer.instance.datamodels[pm.modeltype];
                string insertformart = @"insert into #oysterval:tablename# (#oysterval:uniquecolumn#,#oysterval:columns#)
values(#oysterval:tablename#_seq.nextval,#oysterval:values#) returning #oysterval:uniquecolumn# into :unique_id";

                dictionary vals = new dictionary();
                list parms = new list();

                vals.add("tablename", qs.tablename);
                vals.add("uniquecolumn", qs.uniquecolumn.columnname);
                vals.add("columns", pm.insertcolumns);
                vals.add("values", pm.insertvalues);

                //system.nullable
                var unqtype = qs.uniquecolumn.propertytype;
                if (unqtype.fullname.contains("system.nullable"))
                {
                    var types = unqtype.getgenericarguments();
                    if (types != null && types.length > 0)
                    {
                        unqtype = types[0];
                    }
                }

                var pr = new oracleparameter(":unique_id", activator.createinstance(unqtype));
                pr.direction = parameterdirection.inputoutput;
                parms.add(pr);
                parms.addrange(pm.dataparms);

                string sql = insertformart.tooystertemplate(vals);
                pm.effectcount = dbenginer.instance.executenonquery(sql, parms.toarray());
                pm.effectuniqueids.add(pr.value);
                return pm.effectuniqueids[0];
            }
            else
            {
                throw new exception("请检查传入的editparams,更新列数据不能为空!");
            }

            return null;
        }


里面使用到的其他类型引用,以后会慢慢分享。

而update 则可以这样: uniquecolumn from tablename where condition for update。

将要更新的行select 出来,并且加上update 的锁。保证update按顺序执行,而不会错乱

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn