提交 ecdc31f0 编写于 作者: S szjay

no commit message

上级 314044c2
......@@ -16,6 +16,7 @@ using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
namespace Infrastructure.Utilities
{
......@@ -110,8 +111,23 @@ namespace Infrastructure.Utilities
where = "and " + where;
}
string tableName = DbUtil.GetTableName<T>();
string sql = "select {2}* from {0} where 1=1 {1}".Fmt(tableName, where, top);
string sql = "";
FieldInfo sqlFieldInfo = ReflectHelper.GetField(typeof(T), "SQL");
if (sqlFieldInfo != null) //如果有手工定义SQL,那么优先采用;
{
sql = sqlFieldInfo.GetValue(null).ToString();
if (!top.IsNullOrEmpty())
{
sql = sql.Insert(6, top);
}
}
else //否则自动生成select。
{
string tableName = DbUtil.GetTableName<T>();
sql = "select {2}* from {0} where 1=1 {1}".Fmt(tableName, where, top);
}
DbUtil.Log(sql);
List<T> list = null;
......@@ -129,29 +145,12 @@ namespace Infrastructure.Utilities
public T FirstOrDefault<T>(string where) where T : new()
{
Type type = typeof(T);
string tableName = DbUtil.GetTableName(type);
string sql = "select top 1 * from {0} where 1=1 {1}".Fmt(tableName, where);
DbUtil.Log(sql);
SqlConnection conn = GetConnection(true);
using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
List<T> list = SelectTop<T>(1, where);
if (list.Any())
{
using (DataTable table = new DataTable())
{
da.Fill(table);
if (table.Rows.Count == 0)
{
return default(T);
}
else
{
T obj = table.Rows[0].To<T>();
return obj;
}
}
return list[0];
}
return default(T);
}
public int Insert<T>(T entity)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册