兄弟刚刚写了数据库操作类,请大家提提意见
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++;
}
}
}
}
解决方案 »
- 未能加载类型"URLRewriter"
- 新手诚信请教asp.net c# 操作xml的问题
- 页面上输入大量信息用什么来做?
- 关于url重写,如何重写没有扩展名的路径?
- 怎样解决网站的高并发问题?望高手能提出合适的解决放案
- .NET 开发过BLOG的进
- post提交问题和数据库验证问题?
- 可否根据不同的登录用户设置刷新网页?
- DataGrid中就是更新不了数据,好奇怪?
- 如何将在WIN窗体中写好的网络视频移值到WEB上来?具体如何实现?谢谢了。急。
- 简单的人事管理系统是怎么做的
- 如果什么不点击字体放大放小操作时,能正确打印预览;但一点击后,再点打印预览的话就出现“打印没有权限”?谁之前遇到过类似的问题或知道怎样解决吗?谢谢帮忙!
public static string getconnstr()
{
// string connstr = DBstr.getconnstr("MSSQL");
return DBstr.getconnstr("MSSQL");
//那我来第一个吧:
//没必要申明connstr临时变量 直接返回DBstr.getconnstr("MSSQL")即可
//下边代码没看 太长
}
但是这个要考虑很多,比如字段名和数据库不匹配,关系,延迟加载,特殊类型等等。最好使用现成的 OR 框架,比如 Entity Framework。
http://www.pwmis.com/sqlmap