You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Utils.Attributes
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class DataReferenceAttribute : Attribute
{
public DataReferenceAttribute(string thisTableJoinProperty, Type otherTableType, string needPropert)
{
_ThisTableJoinProperty = thisTableJoinProperty;
_OtherTableType = otherTableType;
_OtherTableJoinProperty = "ID";
_NeedPropert = needPropert;
}
public DataReferenceAttribute(string thisTableJoinProperty, Type otherTableType, string otherTableJoinProperty, string needPropert)
: this(thisTableJoinProperty, otherTableType, needPropert)
{
_OtherTableJoinProperty = otherTableJoinProperty;
}
private string _ThisTableJoinProperty;
public string ThisTableJoinProperty { get { return _ThisTableJoinProperty; } private set { _ThisTableJoinProperty = value; } }
private Type _OtherTableType;
private string _OtherTableJoinProperty;
private string _NeedPropert;
public object GetValue(string thisTablePropertyValue, string connectionStr)
{
if (thisTablePropertyValue.IsNullOrEmpty() || thisTablePropertyValue == "0")
return string.Empty;
var tableName = _OtherTableType.Name;
string sql = "select {0} from {1} where {2} = '{3}'";
sql = string.Format(sql, _NeedPropert, tableName, _OtherTableJoinProperty, thisTablePropertyValue);
SqlDataAdapter da = new SqlDataAdapter(sql, connectionStr);
DataSet ds = new DataSet();
da.Fill(ds);
var table = ds.Tables[0];
if (table.Rows.Count == 0)
return "";
return table.Rows[0][0];//table.Rows[0][_NeedPropert]
}
}
}