1 public string CreatUpdate() 2 { 3 4 StringPlus strclass = new StringPlus(); 5 StringPlus strclass1 = new StringPlus(); 6 StringPlus strclass2 = new StringPlus(); 7 //方法注释 8 strclass.AppendSpaceLine(2, "///"); 9 strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryUpdate"].ToString()); 10 strclass.AppendSpaceLine(2, "/// "); 11 //构建方法指定方法形参为要更改的数据表对应的实体对象 12 strclass.AppendSpaceLine(2, "public bool Update(" + ModelSpace + " model)"); 13 strclass.AppendSpaceLine(2, "{ "); 14 //构建StringBuilder实例装在Sql语句 15 strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); 16 strclass.AppendSpaceLine(3, "strSql.Append(\"update " + _tablename + " set \");"); 17 //表中所有列,获取列名、数据类型、长度、是否只增及是否为主键 18 foreach (ColumnInfo field in Fieldlist) 19 { 20 string columnName = field.ColumnName; 21 string columnType = field.TypeName; 22 string Length = field.Length; 23 bool IsIdentity = field.IsIdentity; 24 bool isPK = field.IsPrimaryKey; 25 26 strclass1.AppendSpaceLine(3, "db.AddInParameter(dbCommand, \"" + columnName + "\", DbType." + CSToProcType(columnType) + ", model." + columnName + ");"); 27 28 if (field.IsIdentity || field.IsPrimaryKey || (Keys.Contains(field))) 29 { 30 continue; 31 } 32 // 构建赋值语句 33 strclass.AppendSpaceLine(3, "strSql.Append(\"" + columnName + "=" + preParameter + columnName + ",\");"); 34 } 35 36 37 //去掉最后的逗号 38 strclass.DelLastComma(); 39 strclass.AppendLine("\");"); 40 //构建Where条件 41 strclass.AppendSpaceLine(3, "strSql.Append(\" where " + GetWhereExpression(Keys) + "\");"); 42 43 //创建命令对象 44 strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); 45 strclass.AppendSpaceLine(3, "DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());"); 46 47 48 strclass.Append(strclass1.Value); 49 //向数据库提交sql语句并返回执行结果 50 strclass.AppendSpaceLine(3, "int rows=db.ExecuteNonQuery(dbCommand);\r\n"); 51 //判断结果并返回相应提示 52 strclass.AppendSpaceLine(3, "if (rows > 0)"); 53 strclass.AppendSpaceLine(3, "{ "); 54 strclass.AppendSpaceLine(4, "return true;"); 55 strclass.AppendSpaceLine(3, "}"); 56 strclass.AppendSpaceLine(3, "else"); 57 strclass.AppendSpaceLine(3, "{ "); 58 strclass.AppendSpaceLine(4, "return false;"); 59 strclass.AppendSpaceLine(3, "}"); 60 61 62 strclass.AppendSpaceLine(2, "}"); 63 return strclass.ToString(); 64 }