生成增改SQL语句

简介

 在做网站时许多的分页面用到新增,修改功能,因数据表不同,每次需要重写sql语句,非常麻烦,自己就想干脆封装成一个公共方法。

主体代码

1 新增

     

/// <summary>
/// 新增
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <param name="dt"></param>
/// <returns></returns>
public string insert<T>(T t)
{
StringBuilder sb = new StringBuilder();
string se = "";
sb.AppendFormat(@"insert into " + t.GetType().Name + " (");
string s = getProperties(t);
s = s.Substring(0, s.Length - 1);

 

string sql = "Values(" + s + ")";

 

foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
{
string de = propertyInfo.Name;
if (de == "id")
{
continue;
}
sb.Append(@"" + de + ",");
}

 

se = sb.ToString().Substring(0, sb.ToString().Length - 1);

 

se = se + ")" + sql;

 

return se;
}

 

public string getProperties<T>(T t)
{
string tStr = string.Empty;
if (t == null)
{
return tStr;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

 

if (properties.Length <= 0)
{
return tStr;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
if (item.Name == "id")
{
continue;
}
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (value != null)
{
Type type = value.GetType();
if (type.Name == "DateTime")
{
value = Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss");
if (value.ToString() == "0001-01-01 00:00:00")
{
value = "null";
}
else if (value.ToString() == "1900-01-01 00:00:00")
{
value = "null";
}
else
{
value = "'" + value + "'";
}
}
else
{
value = "'" + value + "'";
}
}
else
{
value = "'" + value + "'";
}
tStr += string.Format("{0},", value);
}
else
{
getProperties(value);
}
}
return tStr;
}

新增SQL语句可以看成2个部分 第一部分是前面字段名,第二部分是value,insert<T>(T t)方法中通过遍历提取了实体t的字段拼接成第一部分,getProperties<T>(T t)照样通过遍历取出每个字段对应的值拼接成第二部分,然后第一部分连接第二部分得到我们需要sql语句。

二 修改

public string Update<T>(T t, string KeyValue)
{
string tStr = "update " + t.GetType().Name + " set "; ;
if (t == null)
{
return tStr;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

if (properties.Length <= 0)
{
return tStr;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
if (item.Name == "id")
{
continue;
}

string name = item.Name;

object value = item.GetValue(t, null);

if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
if (value != null)
{
Type type = value.GetType();
if (type.Name == "DateTime")
{

value = Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss");

if (value.ToString() == "0001-01-01 00:00:00")
{
value = "null";
}
else if (value.ToString() == "1900-01-01 00:00:00")
{
value = "null";
}
else
{
value = "'" + value + "'";
}
}
else
{
value = "'" + value + "'";
}
}
else
{
value = "'" + value + "'";
}

tStr += item.Name + "= " + value + ",";

}
}
string modelList = tStr.Substring(0, tStr.Length - 1);

modelList += " where id = '" + KeyValue + "'";

return modelList;
}

修改语句也是通过将实体遍历的方式取出字段名与对应的值,然后进行连接,参数KeyValue 为修改数据的ID唯一标识,可以根据需求需改条件。

三 调用代码

 

public ActionResult Sub(Base_group entity,string KeyValue)
{
try
{

 

int IsOk = 0;
string Message = !string.IsNullOrEmpty(KeyValue) ? "编辑成功!" : "新增成功!";
if (!string.IsNullOrEmpty(KeyValue))
{
DatatableToEntity<Base_group> DE = new DatatableToEntity<Base_group>();

 

IsOk = DB.Execute(DE.Update(entity, KeyValue));

 

Log.insert(Base_SysLogBll.OperationType.Update.ToString(), entity.GetType().Name, Message);
}
else
{
IsOk = DB.insert(entity);

 

Log.insert(Base_SysLogBll.OperationType.Add.ToString(), entity.GetType().Name, Message);
}

 

if (IsOk == 0)
{
Message = "操作失败!";
}
var Json = new
{
rows = Message
};

 

return Content(JsonHelper.JsonNew(Json));
}
catch (Exception)
{

 

throw;
}
}

 注意Base_group为实体类,字段名类型需要跟数据库表对应;拼接SQL语句的功能实现,但是只适合数据表唯一标识自增长的数据表;如果唯一标识不是自增长,需要根据需求给唯一标识赋值,并去掉代码   if (item.Name == "id"){continue;}

 

 

总结

  虽然实现了功能,但还是有很多不足之处,如果以后有更好的方法,会进行跟新,希望能帮到需要的人。(纯原创,萌新有不足之处勿喷)

 

posted @ 2019-09-17 17:21  想成大牛的萌新  阅读(208)  评论(2编辑  收藏  举报