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