62,016
社区成员
发帖
与我相关
我的任务
分享
兄弟刚刚写了数据库操作类,请大家提提意见
public static string getconnstr()
{
string connstr = DBstr.getconnstr("MSSQL");
return connstr;
}
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">对象</param>
/// <returns></returns>
public static int Insert(T entity,string sqltext)
{
SqlParameter[] p =DictionarytoArry(Entitylist(entity)) ;
if (p.Length>0)
{
p[0].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(getconnstr(), CommandType.StoredProcedure, sqltext, p);
p = null;
return Convert.ToInt32(p[0].Value);
}
return 0;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="entity">对象</param>
/// <returns></returns>
public static int Update(T entity,string sqltext)
{
SqlParameter[] p = DictionarytoArry(Entitylist(entity));
if (p.Length>0)
{
int i = SqlHelper.ExecuteNonQuery(getconnstr(), CommandType.StoredProcedure, sqltext, p);
p = null;
return i;
}
return 0;
}
/// <summary>
///查询实例
/// </summary>
/// <param name="entity">对象</param>
/// <returns></returns>
public static T Select(T entity, string sqltext)
{
if (entity!=null)
{
if (!string.IsNullOrEmpty(sqltext))
{
SqlParameter[] p = DictionarytoArry(Entitylist(entity));
using (IDataReader dr = SqlHelper.ExecuteReader(getconnstr(), CommandType.StoredProcedure, sqltext, p))
{
while (dr.Read())
{
entity= Select(dr,ref entity);
}
}
p = null;
return entity;
}
}
return default(T);
}
/// <summary>
/// 获取查询集合
/// </summary>
/// <param name="indexpage"></param>
/// <param name="pagesize"></param>
/// <param name="mis"></param>
/// <param name="recordCount"></param>
/// <returns></returns>
public static IList<T> Select(int indexpage, int pagesize,string sqltext, T entity, out int recordCount)
{
if (entity!=null)
{
if (!string.IsNullOrEmpty(sqltext))
{
Dictionary<string,string> dlist = Entitylist(entity);
//增加一个记录记录集总数字段
dlist.Add("recordCount", "0");
SqlParameter[] p = DictionarytoArry(dlist);
p[p.Length-1].Direction=ParameterDirection.Output;
IList<T> list = new List<T>();
using(IDataReader dr=SqlHelper.ExecuteReader(getconnstr(),CommandType.StoredProcedure,sqltext,p))
{
while (dr.Read())
{
list.Add(Select(dr,ref entity));
}
}
recordCount = Convert.ToInt32(p[p.Length-1].Value);
p = null;
return list;
}
}
recordCount = 0;
return null;
}
public static T Select(IDataReader dr, ref T entity)
{
if (dr!=null)
{
Type type = typeof(T);
PropertyInfo[] propertyInfos = type.GetProperties();
//创建PropertyInfo类型变量,获取实例化类的各个属性
for (int i = 0; i < dr.FieldCount; i++)
{
for (int j = 0; j < propertyInfos.Length; j++)
{
PropertyInfo propertyInfo = propertyInfos[j];
if (dr.GetName(i).Trim().ToLower() == propertyInfo.Name.Trim().ToLower())
//实例化类的各个属性与具体的DateReader中的对象名称变为小写字母后进行比较
{
Type propertyType = propertyInfo.PropertyType;
if (propertyType.IsValueType || (propertyType.IsClass && propertyType.IsSerializable && propertyType.IsSealed))
{
object val = dr[propertyInfo.Name];
if (val != DBNull.Value)
{
propertyInfo.SetValue(entity, val, null);
}
break;
}
}
}
}
propertyInfos = null;
return entity;
}
return default(T);
}
public static void parlist(string sqltext, ref Dictionary<string, object> strarr)
{
if (strarr.Count>0 && (!string.IsNullOrEmpty(sqltext)))
{
SqlParameter[] sp = new SqlParameter[strarr.Count];
int i = 0;
foreach (KeyValuePair<string,object> k in strarr)
{
if (k.Value.Equals(""))
{
sp[i] = new SqlParameter("@" + k.Key, k.Value);
}
else
{
sp[i].Direction = ParameterDirection.Output;
}
i++;
}
int t=SqlHelper.ExecuteNonQuery(getconnstr(), CommandType.StoredProcedure, sqltext, sp);
if (t > 0)
{
int j = 0; string spstr;
foreach (KeyValuePair<string,object> k in strarr)
{
if (k.Value.Equals(""))
{
spstr = sp[j].ParameterName.ToString().Replace("@", "");
if (k.Key == spstr)
{
strarr[k.Key] = sp[j];
}
}
j++;
}
}
}
}