小弟刚刚写的数据库操作类,有不足之处请大家多多提意见
兄弟刚刚写了数据库操作类,请大家提提意见
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++;
}
}
}
}
...全文
相关推荐
《鼠小弟-想吃苹果的鼠小弟 (2).ppt
《鼠小弟-想吃苹果的鼠小弟》是一本深受孩子们喜爱的绘本童书,通过生动的故事,向孩子们传递了积极的价值观和生活智慧。故事讲述的是鼠小弟面对一树诱人的苹果,却因为自己的身体条件所限无法获取。
鼠小弟的小背心.pdf
导入主题:教师通过绘本PPT、动物图片和红色A4纸来引起幼儿对故事的兴趣,提前准备好绘本《鼠小弟的小背心》,并利用活动纸偶来引导孩子观察故事中鼠小弟的小背心。
可爱的鼠小弟05鼠小弟,鼠小弟_精美学习课件ppt
例如,在“可爱的鼠小弟”的PPT中,可以通过简单平滑的动画展示鼠小弟的动作,让孩子们更容易被吸引。 3.
可爱的鼠小弟04鼠小弟和鼠小妹_精美学习课件ppt
### “可爱的鼠小弟04鼠小弟和鼠小妹”PPT可能涉及的具体知识点1. **故事背景**:首先,这个PPT可能会介绍“鼠小弟”系列故事的背景,包括鼠小弟和鼠小妹的角色设定、性格特点等。2.
《鼠小弟,鼠小弟》绘本故事PPT模板
《鼠小弟,鼠小弟》绘本故事PPT 鼠小弟,你站上来看看。哈哈,指针没动


