動態提交表單時,如何擷取參數類型

來源:互聯網
上載者:User

    如題,首先為什麼要動態提交表單,我們經常需要遍曆表單並提交資料,這樣可以節省大量時間。

    再者,為什麼要使用參數方法提交,最主要的是參數方式提交可以增強安全性,避免注入式攻擊,好處這裡就不多說了

    問題:動態提交時,不論是ms sql 還是 oracle 都需要知道參數類型,如何動態擷取參數類型。

    直接貼代碼:

代碼

 public OracleType FieldOracleType(string field, string table)
        {

            string type = FieldAttribute(field, table).Rows[0][0].ToString().ToLower() ;

            string[] types = Enum.GetNames(typeof(OracleType));
            foreach (string lx in types)
            {
                if (type == lx.ToLower())
                {
                    type = lx;
                    break;
                }
                else if (type == "varchar2" | type == "nvarchar2")
                {
                    type = "VarChar";
                    break;
                }
                else if (type == "date")
                {
                    type = "DateTime";
                    break;
                }
                else if (type.IndexOf("TIMESTAMP".ToLower()) >= 0)
                {
                    type = "DateTime";
                    break;
                }
            }

            return (OracleType)Enum.Parse(typeof(OracleType), type);

        }

  

如上,通過這個方法,我們只需要傳表和欄位名,就可以輕公獲到參數類型,

其中public DataTable FieldAttribute(string field, string table)方法是返回欄位屬性的

如下:

 

代碼

        /// <summary>
        ///返回欄位的屬性
         /// </summary>
        /// <param name="field"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        public DataTable FieldAttribute(string field, string table)
        {
            if (ColumnExists(table, field))
            {
                string sql = "select data_type,user_tab_columns.DATA_LENGTH,user_tab_columns.DATA_PRECISION,user_tab_columns.DATA_SCALE  from user_tab_columns where table_name ='" + table.ToUpper() + "' and user_tab_columns.COLUMN_NAME='" + field.ToUpper() + "'";
                DataTable dt = Query(sql).Tables[0];
                return dt;

            }
            else
            {
                return null;
            }
        }

 

 

調於樣本:

 OracleParameter xtpt1 = new OracleParameter("參數名", bllcom.FieldOracleType("欄位名", 表名));
 xtpt1.Value = "值"; 

 

Ms Sql同上。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.