C#自動給據sql中的帶@的變數提取變數名稱在從簡單資料對象中取得產生SqlParameter數組進行資料插入(利用反射完成)

來源:互聯網
上載者:User

C#自動給據sql中的帶@的變數提取變數名稱在從簡單資料對象中取得產生SqlParameter數組進行資料插入



//自動取得sql中的帶@的變數,產生SqlParameter數組,從簡單資料對象中取得sql中所需變數值

public class ziDongGetSqlPara
{
    #region   public static object[] getParArr(string strSql, object dataObj)
    /// <summary>
    /// 資料庫操作sql 用SqlParameter數組取得
    /// </summary>
    /// <param name="strSql">sql語句</param>
    /// <param name="dataObj">sql查詢用變數資料存放對象</param>
    /// <returns>SqlParameter數組</returns>
    public static object[] getParArr(string strSql, object dataObj)
    {

        object[] retObj = new object[1];
        //string strSql = "update Tablname set tb_password=@tb_password    where    tb_password=@oldtb_password    and    tb_username=@tb_username where    dd=@dd    and    @f_ff=dfffddd    and    @中文欄位名稱=T中文欄位名稱f and @resr=@etet222";
        Regex rg = new Regex("@[^,\t /=)(;-]*", RegexOptions.IgnoreCase);
        MatchCollection matCol = rg.Matches(strSql);
        SqlParameter[] sqlParArr = new SqlParameter[matCol.Count];
        int i = 0;
        foreach (Match mt in matCol)
        {
            sqlParArr[i++] = new SqlParameter(mt.Value, getFieldValue(mt.Value.ToString().TrimStart('@'), dataObj));
        }
        retObj[0] = sqlParArr;
        return retObj;
    }
    #endregion

    #region public static string getFieldValue(string strObjName, object dataObj) 取得指定對象中指定名稱的變數或屬性名稱
    /// <summary>
    /// 取得指定對象中指定名稱的變數或屬性名稱
    /// </summary>
    /// <param name="strObjName">變數或屬性名稱</param>
    /// <param name="dataObj">對象</param>
    /// <returns></returns>
    public static string getFieldValue(string strObjName, object dataObj)
    {
        string strRet = "";
        Type t = dataObj.GetType();
        if (t.FullName.ToString().Equals("System.Collections.Hashtable"))
        {
            strRet = Convert.ToString(((Hashtable)dataObj)[strObjName]);
        }
        else
        {
            PropertyInfo method_1 = t.GetProperty(strObjName);//方法的名稱
            if (method_1 != null)
            {
                strRet = Convert.ToString(method_1.GetGetMethod().Invoke(dataObj, null));//屬性值取得
            }
            else
            {
                FieldInfo fi = t.GetField(strObjName);//變數欄位對象取得
                if (fi != null)
                {
                    strRet = Convert.ToString(fi.GetValue(dataObj));//變數值取得
                }
            }
        }
        return strRet;
    }
    #endregion

    #region public static int dataInsert(string strSql, string dataBeanObj) 資料插入
    /// <summary>
    /// 資料插入
    /// </summary>
    /// <param name="strSql">插入sql</param>
    /// <param name="dataBeanObj">插入sql用變數存放對象</param>
    /// <returns></returns>
    public static int dataInsert(string strSql, object dataBeanObj)
    {
        int iret = 0;
        SqlParameter[] sqlParArr = getParArr(strSql, dataBeanObj)[0] as SqlParameter[];
        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, strSql, sqlParArr);
        return iret;
    }
    #endregion

}

//簡單資料對象類

public class logShow
{
    // pkId, pcontent, createdate, errmesssge
    private string _pcontent;

    public string pcontent
    {
        get { return _pcontent; }
        set { _pcontent = value; }
    }
    private string _errmesssge;

    public string errmesssge
    {
        get { return _errmesssge; }
        set { _errmesssge = value; }
    }

}

//測試代碼

        logShow logShowDataBean = new logShow();
        logShowDataBean.errmesssge = "http://www.my400800.cn";
        logShowDataBean.pcontent = "http://www.my400800.cn 是一個不錯的網站。";
        ziDongGetSqlPara.dataInsert("insert into tb_log(pcontent,errmesssge)values(@pcontent,@errmesssge)", logShowDataBean);

//需要添加的引用

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using fjt.DBUtility;

//根據簡單資料對象取得Sql用變數的值

        ziDongGetSqlPara.getFieldValue("strPri1", t1);
        ziDongGetSqlPara.getFieldValue("strPri2", t1);
        ziDongGetSqlPara.getFieldValue("StrGetSetTest", t1);

相關文章

聯繫我們

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