asp.net Oracle 之Insert 與 Update 更新封閉代碼

來源:互聯網
上載者:User

asp教程.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<string, string> vals = new dictionary<string, string>();
                list<idataparameter> parms = new list<idataparameter>();

                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 則可以這樣:select uniquecolumn from tablename where condition for update。

將要更新的行select 出來,並且加上update 的鎖。保證update按順序執行,而不會錯亂

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.