C#自动给据sql中的带@的变量提取变量名称在从简单数据对象中取得生成SqlParameter数组进行数据插入(利用反射完成)

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);

posted @ 2009-08-20 13:37  ljlxyf  阅读(401)  评论(0编辑  收藏  举报