/// <summary> /// 得到Delete的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatDelete() { StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 删除一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public void Delete(" + LTP.CodeHelper.CodeCommon.GetInParameter(Keys) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); if (dbobj.DbType != "OleDb") { strclass.AppendSpaceLine(3, "strSql.Append(\"delete " + _tablename + " \");"); } else { strclass.AppendSpaceLine(3, "strSql.Append(\"delete from " + _tablename + " \");"); } strclass.AppendSpaceLine(3, "strSql.Append(\" where " + LTP.CodeHelper.CodeCommon.GetWhereExpression(Keys) + "\" );"); strclass.AppendSpaceLine(3, DbHelperName + ".ExecuteSql(strSql.ToString());"); strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
/// <summary> /// 得到Delete的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatDelete() { StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 删除一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public void Delete(" + LTP.CodeHelper.CodeCommon.GetInParameter(Keys) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); //if (dbobj.DbType != "OleDb") //{ // strclass.AppendSpaceLine(3, "strSql.Append(\"delete " + _tablename + " \");" ); //} //else //{ strclass.AppendSpaceLine(3, "strSql.Append(\"delete from " + _tablename + " \");"); //} strclass.AppendSpaceLine(3, "strSql.Append(\" where " + LTP.CodeHelper.CodeCommon.GetWhereExpression(Keys) + "\" );"); strclass.AppendSpaceLine(3, "DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());"); strclass.AppendSpaceLine(3, "db.ExecuteNonQuery(dbCommand);"); strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
/// <summary> /// 得到增加Add()的代码 /// </summary> public string CreatAdd() { StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); StringPlus strclass3 = new StringPlus(); StringPlus strclass4 = new StringPlus(); strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 增加一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strretu = "int"; } //方法定义头 string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); //重新定义方法头 if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "return ExecuteInsert(\"Insert" + ModelName + "\", model);"); } else { strclass.AppendSpaceLine(3, "ExecuteInsert(\"Insert" + ModelName + "\", model);"); } strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
public string CreatGetListArray() { string strList = "List<" + ModelSpace + ">"; StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 获得数据列表(比DataSet效率高,推荐使用)"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public " + strList + " GetListArray(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, strList + " list = new " + strList + "();"); strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); strclass.AppendSpaceLine(3, "using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "while (dataReader.Read())"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "list.Add(ReaderBind(dataReader));"); strclass.AppendSpaceLine(4, "}"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return list;"); strclass.AppendSpaceLine(2, "}"); return(strclass.Value); }
public string GetListAspxCs() { StringPlus stringPlus = new StringPlus(); stringPlus.AppendLine(); stringPlus.AppendSpace(2, this.BLLSpace + " bll = new " + this.BLLSpace + "();"); return(stringPlus.ToString()); }
/// <summary> /// 得到GetList()的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatGetList() { StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 获得数据列表"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); if ((dbobj.DbType == "SQL2000") || (dbobj.DbType == "SQL2005") || (dbobj.DbType == "SQL2008")) { strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 获得前几行数据"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(int Top,string strWhere,string filedOrder)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"select \");"); strclass.AppendSpaceLine(3, "if(Top>0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" top \"+Top.ToString());"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" " + Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" order by \" + filedOrder);"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); } return(strclass.Value); }
/// <summary> /// 得到Update()的代码 /// </summary> /// <param name="DbName"></param> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <param name="ModelName"></param> /// <returns></returns> public string CreatUpdate() { //if (ModelSpace == "") //{ // ModelSpace = "ModelClassName"; ; //} StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 更新一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public void Update(" + ModelSpace + " model)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"update " + _tablename + " set \");"); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; string Length = field.Length; bool IsIdentity = field.IsIdentity; bool isPK = field.IsPK; if (field.IsIdentity || field.IsPK || (Keys.Contains(field))) { continue; } if (CodeCommon.IsAddMark(columnType.Trim())) { strclass.AppendSpaceLine(3, "strSql.Append(\"" + columnName + "='\"+model." + columnName + "+\"',\");"); } else { strclass.AppendSpaceLine(3, "strSql.Append(\"" + columnName + "=\"+model." + columnName + "+\",\");"); } } //去掉最后的逗号 strclass.Remove(strclass.Value.Length - 6, 1); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + LTP.CodeHelper.CodeCommon.GetModelWhereExpression(Keys) + "\");"); strclass.AppendSpaceLine(3, "DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());"); strclass.AppendSpaceLine(3, "db.ExecuteNonQuery(dbCommand);"); strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
public string CreatGetList() { StringPlus stringPlus = new StringPlus(); stringPlus.AppendSpaceLine(2, "/// <summary>"); stringPlus.AppendSpaceLine(2, "/// " + this.Languagelist["summaryGetList"].ToString()); stringPlus.AppendSpaceLine(2, "/// </summary>"); stringPlus.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); stringPlus.AppendSpaceLine(2, "{"); stringPlus.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); stringPlus.AppendSpace(3, "strSql.Append(\"select "); stringPlus.AppendLine(this.Fieldstrlist + " \");"); stringPlus.AppendSpaceLine(3, "strSql.Append(\" FROM " + this.TableName + " \");"); stringPlus.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); stringPlus.AppendSpaceLine(3, "{"); stringPlus.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); stringPlus.AppendSpaceLine(3, "}"); stringPlus.AppendSpaceLine(3, "return " + this.DbHelperName + ".Query(strSql.ToString());"); stringPlus.AppendSpaceLine(2, "}"); if (this.dbobj.DbType == "SQL2000" || this.dbobj.DbType == "SQL2005" || this.dbobj.DbType == "SQL2008" || this.dbobj.DbType == "SQL2012") { stringPlus.AppendLine(); stringPlus.AppendSpaceLine(2, "/// <summary>"); stringPlus.AppendSpaceLine(2, "/// " + this.Languagelist["summaryGetList2"].ToString()); stringPlus.AppendSpaceLine(2, "/// </summary>"); stringPlus.AppendSpaceLine(2, "public DataSet GetList(int Top,string strWhere,string filedOrder)"); stringPlus.AppendSpaceLine(2, "{"); stringPlus.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); stringPlus.AppendSpaceLine(3, "strSql.Append(\"select \");"); stringPlus.AppendSpaceLine(3, "if(Top>0)"); stringPlus.AppendSpaceLine(3, "{"); stringPlus.AppendSpaceLine(4, "strSql.Append(\" top \"+Top.ToString());"); stringPlus.AppendSpaceLine(3, "}"); stringPlus.AppendSpaceLine(3, "strSql.Append(\" " + this.Fieldstrlist + " \");"); stringPlus.AppendSpaceLine(3, "strSql.Append(\" FROM " + this.TableName + " \");"); stringPlus.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); stringPlus.AppendSpaceLine(3, "{"); stringPlus.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); stringPlus.AppendSpaceLine(3, "}"); stringPlus.AppendSpaceLine(3, "strSql.Append(\" order by \" + filedOrder);"); stringPlus.AppendSpaceLine(3, "return " + this.DbHelperName + ".Query(strSql.ToString());"); stringPlus.AppendSpaceLine(2, "}"); } return(stringPlus.Value); }
/// <summary> /// 得到Exists方法的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="ID"></param> /// <returns></returns> public string CreatExists() { StringPlus strclass = new StringPlus(); if (_keys.Count > 0) { strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 是否存在该记录"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool Exists(" + LTP.CodeHelper.CodeCommon.GetInParameter(Keys) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"select count(1) from " + _tablename + "\");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + LTP.CodeHelper.CodeCommon.GetWhereExpression(Keys) + "\");"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Exists(strSql.ToString());"); strclass.AppendSpace(2, "}"); } return(strclass.ToString()); }
public string CreatAdd() { StringPlus stringPlus = new StringPlus(); new StringPlus(); new StringPlus(); new StringPlus(); new StringPlus(); stringPlus.AppendLine(); stringPlus.AppendSpaceLine(2, "/// <summary>"); stringPlus.AppendSpaceLine(2, "/// " + this.Languagelist["summaryadd"].ToString()); stringPlus.AppendSpaceLine(2, "/// </summary>"); string text = "void"; if ((this.dbobj.DbType == "SQL2000" || this.dbobj.DbType == "SQL2005" || this.dbobj.DbType == "SQL2008" || this.dbobj.DbType == "SQL2012") && this.IsHasIdentity) { text = "int"; } string text2 = string.Concat(new string[] { CodeCommon.Space(2), "public ", text, " Add(", this.ModelSpace, " model)" }); stringPlus.AppendLine(text2); stringPlus.AppendSpaceLine(2, "{"); if ((this.dbobj.DbType == "SQL2000" || this.dbobj.DbType == "SQL2005" || this.dbobj.DbType == "SQL2008" || this.dbobj.DbType == "SQL2012") && this.IsHasIdentity) { stringPlus.AppendSpaceLine(3, "return ExecuteInsert(\"Insert" + this.ModelName + "\", model);"); } else { stringPlus.AppendSpaceLine(3, "ExecuteInsert(\"Insert" + this.ModelName + "\", model);"); } stringPlus.AppendSpace(2, "}"); return(stringPlus.ToString()); }
/// <summary> /// 得到Exists方法的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="ID"></param> /// <returns></returns> public string CreatExists() { StringPlus strclass = new StringPlus(); if (_keys.Count > 0) { strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 是否存在该记录"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool Exists(" + LTP.CodeHelper.CodeCommon.GetInParameter(Keys) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); strclass.AppendSpaceLine(3, "StringBuilder strSql = new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select count(1) from " + _tablename); strclass.AppendLine(" where " + GetWhereExpression(Keys) + "\");"); strclass.AppendSpaceLine(3, "DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());"); strclass.Append(GetPreParameter(Keys)); strclass.AppendSpaceLine(3, "int cmdresult;"); strclass.AppendSpaceLine(3, "object obj = db.ExecuteScalar(dbCommand);"); strclass.AppendSpaceLine(3, "if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "cmdresult = 0;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "cmdresult = int.Parse(obj.ToString());"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "if (cmdresult == 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(2, "}"); } return(strclass.Value); }
/// <summary> /// 得到GetList()的代码 /// </summary> public string CreatGetList() { StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 获得数据列表"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableNameParent + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); return(strclass.Value); }
/// <summary> /// 得到GetList()的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatGetList() { StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 获得数据列表"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); strclass.AppendSpaceLine(3, "return db.ExecuteDataSet(CommandType.Text, strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); return(strclass.Value); }
/// <summary> /// 得到GetList()的代码 /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatGetList() { //StringPlus strclass = new StringPlus(); //strclass.AppendSpaceLine(2, "/// <summary>"); //strclass.AppendSpaceLine(2, "/// 获取数据列表"); //strclass.AppendSpaceLine(2, "/// </summary>"); //strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); //strclass.AppendSpaceLine(2, "{"); ////strclass.AppendSpaceLine(3, "" + DbParaHead + "Parameter[] parameters = {"); ////strclass.AppendSpaceLine(5, "new " + DbParaHead + "Parameter(\"" + preParameter + _key + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, _keyType, "") + ")"); ////strclass.AppendSpaceLine(4, "};"); ////strclass.AppendSpaceLine(3, "parameters[0].Value = strWhere;"); //strclass.AppendSpaceLine(3, "return " + DbHelperName + ".RunProcedure(\"" + ProcPrefix + _tablename + "_GetList" + "\",null,\"ds\");"); //strclass.AppendSpaceLine(2, "}"); //return strclass.Value; StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 获得数据列表"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); return(strclass.Value); }
/// <summary> /// �õ�GetModel()�Ĵ��� /// </summary> /// <param name="DbName"></param> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <param name="ModelName"></param> /// <returns></returns> public string CreatGetModel() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); // strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetModel"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public " + ModelSpace + " GetModel(" + Maticsoft.CodeHelper.CodeCommon.GetInParameter(Keys, true) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, KeysNullTip); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); if (dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") { strclass.Append(" top 1 "); } strclass.AppendLine(Fieldstrlist + " from " + _tablename + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + CodeCommon.GetWhereParameterExpression(Keys, true, dbobj.DbType) + "\");"); strclass.AppendLine(CodeCommon.GetPreParameter(Keys, true, dbobj.DbType)); strclass.AppendSpaceLine(3, "" + ModelSpace + " model=new " + ModelSpace + "();"); strclass.AppendSpaceLine(3, "DataSet ds=" + DbHelperName + ".Query(strSql.ToString(),parameters);"); strclass.AppendSpaceLine(3, "if(ds.Tables[0].Rows.Count>0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return DataRowToModel(ds.Tables[0].Rows[0]);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return null;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(2, "}"); return strclass.ToString(); }
/// <summary> /// 得到Update()的代码 /// </summary> /// <param name="DbName"></param> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <param name="ModelName"></param> /// <returns></returns> public string CreatUpdate() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 更新一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public void Update(" + ModelSpace + " model)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"update " + _tablename + " set \");"); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; string Length = field.Length; bool IsIdentity = field.IsIdentity; bool isPK = field.IsPK; if (field.IsIdentity || field.IsPK || (Keys.Contains(field))) { continue; } strclass.AppendSpaceLine(3, "if (model." + columnName + " != null)"); strclass.AppendSpaceLine(3, "{"); if ((dbobj.DbType == "Oracle") && (columnType.ToLower() == "date" || columnType.ToLower() == "datetime")) { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "=to_date('\" + model." + columnName + ".ToString() + \"','YYYY-MM-DD HH24:MI:SS'),\");"); } else if (columnType.ToLower() == "bit") { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "=\"+ (model." + columnName + "? 1 : 0) +\",\");"); } else if (CodeCommon.IsAddMark(columnType.Trim())) { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "='\"+model." + columnName + "+\"',\");"); } else { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "=\"+model." + columnName + "+\",\");"); } strclass.AppendSpaceLine(3, "}"); } //去掉最后的逗号 strclass.AppendSpaceLine(3, "int n = strSql.ToString().LastIndexOf(\",\");"); strclass.AppendSpaceLine(3, "strSql.Remove(n, 1);"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + LTP.CodeHelper.CodeCommon.GetModelWhereExpression(Keys) + "\");"); strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSql(strSql.ToString());"); strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
/// <summary> /// �õ���ʾ���б����Ĵ��� /// </summary> public string GetListAspxCs() { StringPlus strclass = new StringPlus(); strclass.AppendLine(); strclass.AppendSpace(2, BLLSpace + " bll = new " + BLLSpace + "();"); return strclass.ToString(); }
/// <summary> /// �õ�GetList()�Ĵ��� /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatGetList() { StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); if ((dbobj.DbType == "SQL2000") || (dbobj.DbType == "SQL2005") || (dbobj.DbType == "SQL2008") || (dbobj.DbType == "SQL2012")) { strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList2"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(int Top,string strWhere,string filedOrder)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"select \");"); strclass.AppendSpaceLine(3, "if(Top>0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" top \"+Top.ToString());"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" " + Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" order by \" + filedOrder);"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); } //����List<> strclass.AppendLine(); // ���峬 2015-10-13 strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public List<" + ModelSpace + "> GetModelList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "DataSet ds = GetList(strWhere);"); strclass.AppendSpaceLine(3, "return DataTableToList(ds.Tables[0]);"); strclass.AppendSpaceLine(2, "}"); //����List<> strclass.AppendLine();// ���峬 2015-10-13 strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public List<" + ModelSpace + "> DataTableToList(DataTable dt)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "List<" + ModelSpace + "> modelList = new List<" + ModelSpace + ">();"); strclass.AppendSpaceLine(3, "int rowsCount = dt.Rows.Count;"); strclass.AppendSpaceLine(3, "if (rowsCount > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, ModelSpace + " model;"); strclass.AppendSpaceLine(4, "for (int n = 0; n < rowsCount; n++)"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model = DataRowToModel(dt.Rows[n]);"); strclass.AppendSpaceLine(5, "if (model != null)"); strclass.AppendSpaceLine(5, "{"); strclass.AppendSpaceLine(6, "modelList.Add(model);"); strclass.AppendSpaceLine(5, "}"); strclass.AppendSpaceLine(4, "}"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return modelList;"); strclass.AppendSpaceLine(2, "}"); return strclass.Value; }
/// <summary> /// �õ�Delete�Ĵ��� /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatDelete() { StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryDelete"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool Delete(" + Maticsoft.CodeHelper.CodeCommon.GetInParameter(Keys, true) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); //if (dbobj.DbType != "OleDb") //{ // strclass.AppendSpaceLine(3, "strSql.Append(\"delete " + _tablename + " \");" ); //} //else //{ strclass.AppendSpaceLine(3, "strSql.Append(\"delete from " + _tablename + " \");"); //} strclass.AppendSpaceLine(3, "strSql.Append(\" where " + Maticsoft.CodeHelper.CodeCommon.GetWhereExpression(Keys, true) + "\" );"); strclass.AppendSpaceLine(3, "int rowsAffected=" + DbHelperName + ".ExecuteSql(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (rowsAffected > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpace(2, "}"); #region �����������ȵ�ɾ��(���б�ʶ�ֶΣ����зDZ�ʶ�����ֶ�) if ((Maticsoft.CodeHelper.CodeCommon.HasNoIdentityKey(Keys)) && (Maticsoft.CodeHelper.CodeCommon.GetIdentityKey(Keys) != null)) { strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryDelete"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool Delete(" + Maticsoft.CodeHelper.CodeCommon.GetInParameter(Keys, false) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, KeysNullTip); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"delete from " + _tablename + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + GetWhereExpression(Keys, false) + "\");"); strclass.AppendLine(GetPreParameter(Keys, false)); strclass.AppendSpaceLine(3, "int rows=" + DbHelperName + ".ExecuteSql(strSql.ToString(),parameters);"); strclass.AppendSpaceLine(3, "if (rows > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(2, "}"); } #endregion #region ����ɾ������ string keyField = ""; if (Keys.Count == 1) { keyField = Keys[0].ColumnName; } else { foreach (ColumnInfo field in Keys) { if (field.IsIdentity) { keyField = field.ColumnName; break; } } } if (keyField.Trim().Length > 0) { strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryDeletelist"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool DeleteList(string " + keyField + "list )"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"delete from " + _tablename + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + keyField + " in (\"+" + keyField + "list + \") \");"); strclass.AppendSpaceLine(3, "int rows=" + DbHelperName + ".ExecuteSql(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (rows > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(2, "}"); } #endregion return strclass.ToString(); }
public string GetDALCode(bool Maxid, bool Exists, bool Add, bool Update, bool Delete, bool GetModel, bool List) { StringPlus strclass = new StringPlus(); strclass.AppendLine("using System;"); strclass.AppendLine("using System.Data;"); strclass.AppendLine("using System.Text;"); strclass.AppendLine("using System.Collections.Generic;"); switch (dbobj.DbType) { case "SQL2005": case "SQL2008": case "SQL2012": strclass.AppendLine("using System.Data.SqlClient;"); break; case "SQL2000": strclass.AppendLine("using System.Data.SqlClient;"); break; case "Oracle": strclass.AppendLine("using System.Data.OracleClient;"); break; case "MySQL": strclass.AppendLine("using MySql.Data.MySqlClient;"); break; case "OleDb": strclass.AppendLine("using System.Data.OleDb;"); break; case "SQLite": strclass.AppendLine("using System.Data.SQLite;"); break; } if (IDALpath != "") { strclass.AppendLine("using " + IDALpath + ";"); } // strclass.AppendLine("using Maticsoft.DBUtility;//Please add references"); // ���峬 2014-10-21 ��Ϊ����Ŀ���ݿ���� strclass.AppendLine("using " + NameSpace + ".DBUtility;"); strclass.AppendLine("namespace " + DALpath); strclass.AppendLine("{"); strclass.AppendSpaceLine(1, "/// <summary>"); strclass.AppendSpaceLine(1, "/// " + Languagelist["summary"].ToString() + ":" + DALName); strclass.AppendSpaceLine(1, "/// </summary>"); strclass.AppendSpace(1, "public partial class " + DALName); if (IClass != "") { strclass.Append(":" + IClass); } strclass.AppendLine(""); strclass.AppendSpaceLine(1, "{"); strclass.AppendSpaceLine(2, "public " + DALName + "()"); strclass.AppendSpaceLine(2, "{}"); strclass.AppendSpaceLine(2, "#region Method"); #region �������� if (Maxid) { strclass.AppendLine(CreatGetMaxID()); } if (Exists) { strclass.AppendLine(CreatExists()); } if (Add) { strclass.AppendLine(CreatAdd()); } if (Update) { strclass.AppendLine(CreatUpdate()); } if (Delete) { strclass.AppendLine(CreatDelete()); } if (GetModel) { strclass.AppendLine(CreatGetModel()); strclass.AppendLine(CreatDataRowToModel()); } if (List) { strclass.AppendLine(CreatGetList()); strclass.AppendLine(CreatGetListByPage()); strclass.AppendLine(CreatGetListByPageProc()); } #endregion strclass.AppendSpaceLine(2, "#endregion Method"); //strclass.AppendSpaceLine(2, "#region MethodEx"); //strclass.AppendLine(""); //strclass.AppendSpaceLine(2, "#endregion MethodEx"); strclass.AppendSpaceLine(1, "}"); strclass.AppendLine("}"); strclass.AppendLine(""); return strclass.ToString(); }
/// <summary> /// �õ�GetModel()�Ĵ��� /// </summary> /// <param name="DbName"></param> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <param name="ModelName"></param> /// <returns></returns> public string CreatGetModel() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetModel"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public " + ModelSpace + " GetModel(" + Maticsoft.CodeHelper.CodeCommon.GetInParameter(Keys, true) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); if (dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") { strclass.Append(" top 1 "); } strclass.AppendLine(" \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" " + Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" from " + _tablename + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + Maticsoft.CodeHelper.CodeCommon.GetWhereExpression(Keys, true) + "\" );"); strclass.AppendSpaceLine(3, ModelSpace + " model=new " + ModelSpace + "();"); strclass.AppendSpaceLine(3, "DataSet ds=" + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(3, "if(ds.Tables[0].Rows.Count>0)"); strclass.AppendSpaceLine(3, "{"); #region /* foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"]!=null && ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); #region switch (CodeCommon.DbTypeToCS(columnType)) { case "int": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=int.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); //strclass.AppendSpaceLine(4, "}"); } break; case "long": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=long.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); //strclass.AppendSpaceLine(4, "}"); } break; case "decimal": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=decimal.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); //strclass.AppendSpaceLine(4, "}"); } break; case "float": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=float.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); //strclass.AppendSpaceLine(4, "}"); } break; case "DateTime": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=DateTime.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); //strclass.AppendSpaceLine(4, "}"); } break; case "string": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"]!=null)"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString();"); //strclass.AppendSpaceLine(4, "}"); } break; case "bool": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "if((ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()==\"1\")||(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString().ToLower()==\"true\"))"); strclass.AppendSpaceLine(5, "{"); strclass.AppendSpaceLine(6, "model." + columnName + "=true;"); strclass.AppendSpaceLine(5, "}"); strclass.AppendSpaceLine(5, "else"); strclass.AppendSpaceLine(5, "{"); strclass.AppendSpaceLine(6, "model." + columnName + "=false;"); strclass.AppendSpaceLine(5, "}"); //strclass.AppendSpaceLine(4, "}"); } break; case "byte[]": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=(byte[])ds.Tables[0].Rows[0][\"" + columnName + "\"];"); //strclass.AppendSpaceLine(4, "}"); } break; case "uniqueidentifier": case "Guid": { //strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); //strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=new Guid(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); //strclass.AppendSpaceLine(4, "}"); } break; default: strclass.AppendSpaceLine(5, "//model." + columnName + "=ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString();"); break; } #endregion strclass.AppendSpaceLine(4, "}"); } */ #endregion strclass.AppendSpaceLine(4, "return DataRowToModel(ds.Tables[0].Rows[0]);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return null;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpace(2, "}"); return strclass.ToString(); }
/// <summary> /// 得到Add()的代码 /// </summary> public string CreatAdd() { //if (ModelSpace == "") //{ // ModelSpace = "ModelClassName"; ; //} StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 增加一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strretu = "int"; } //方法定义头 string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "Database db = DatabaseFactory.CreateDatabase();"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + _tablename + "(\");"); strclass1.AppendSpace(3, "strSql.Append(\""); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; if (IsIdentity) { continue; } strclass1.Append(columnName + ","); if (CodeCommon.IsAddMark(columnType.Trim())) { strclass2.AppendSpaceLine(3, "strSql.Append(\"'\"+model." + columnName + "+\"',\");"); } else { strclass2.AppendSpaceLine(3, "strSql.Append(\"\"+model." + columnName + "+\",\");"); } } //去掉最后的逗号 strclass1.DelLastComma(); strclass2.Remove(strclass2.Value.Length - 6, 1); strclass1.AppendLine("\");"); strclass.Append(strclass1.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.Append(strclass2.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); //重新定义方法头 if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select @@IDENTITY\");"); strclass.AppendSpaceLine(3, "DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());"); strclass.AppendSpaceLine(3, "int result;"); strclass.AppendSpaceLine(3, "object obj = db.ExecuteScalar(dbCommand);"); strclass.AppendSpaceLine(3, "if(!int.TryParse(obj.ToString(),out result))"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return 0;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return result;"); } else { strclass.AppendSpaceLine(3, "DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());"); strclass.AppendSpaceLine(3, "db.ExecuteNonQuery(dbCommand);"); } strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
public string GetDALCode(bool Maxid, bool Exists, bool Add, bool Update, bool Delete, bool GetModel, bool List) { StringPlus strclass = new StringPlus(); strclass.AppendLine("using System;"); strclass.AppendLine("using System.Data;"); strclass.AppendLine("using System.Text;"); strclass.AppendLine("using System.Collections.Generic;"); strclass.AppendLine("using Microsoft.Practices.EnterpriseLibrary.Data;"); strclass.AppendLine("using Microsoft.Practices.EnterpriseLibrary.Data.Sql;"); strclass.AppendLine("using System.Data.Common;"); if (IDALpath != "") { strclass.AppendLine("using " + IDALpath + ";"); } strclass.AppendLine("using Maticsoft.DBUtility;//请先添加引用"); strclass.AppendLine("namespace " + DALpath); strclass.AppendLine("{"); strclass.AppendSpaceLine(1, "/// <summary>"); strclass.AppendSpaceLine(1, "/// 数据访问类" + DALName + "。"); strclass.AppendSpaceLine(1, "/// </summary>"); strclass.AppendSpace(1, "public class " + DALName); if (IClass != "") { strclass.Append(":" + IClass); } strclass.AppendLine(""); strclass.AppendSpaceLine(1, "{"); strclass.AppendSpaceLine(2, "public " + DALName + "()"); strclass.AppendSpaceLine(2, "{}"); strclass.AppendSpaceLine(2, "#region 成员方法"); #region 方法代码 if (Maxid) { strclass.AppendLine(CreatGetMaxID()); } if (Exists) { strclass.AppendLine(CreatExists()); } if (Add) { strclass.AppendLine(CreatAdd()); } if (Update) { strclass.AppendLine(CreatUpdate()); } if (Delete) { strclass.AppendLine(CreatDelete()); } if (GetModel) { strclass.AppendLine(CreatGetModel()); } if (List) { strclass.AppendLine(CreatGetList()); strclass.AppendLine(CreatGetListByPageProc()); strclass.AppendLine(CreatGetListArray()); strclass.AppendLine(CreatReaderBind()); } #endregion strclass.AppendSpaceLine(2, "#endregion 成员方法"); strclass.AppendSpaceLine(1, "}"); strclass.AppendLine("}"); strclass.AppendLine(""); return(strclass.ToString()); }
/// <summary> /// 得到Add()的代码 /// </summary> public string CreatAdd() { //if (ModelSpaceParent == "") //{ // ModelSpaceParent = "ModelClassName"; ; //} StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); StringPlus strclass3 = new StringPlus(); StringPlus strclass4 = new StringPlus(); strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 增加一条数据,及其子表数据"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strretu = "int"; } //方法定义头 string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpaceParent + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + _tablenameparent + "(\");"); strclass1.AppendSpace(3, "strSql.Append(\""); int n = 0; int nkey = 0; foreach (ColumnInfo field in FieldlistParent) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; string Length = field.Length; if (field.IsIdentity) { //nkey = n; continue; } strclass3.AppendSpaceLine(5, "new " + DbParaHead + "Parameter(\"" + preParameter + columnName + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, columnType, Length) + "),"); strclass1.Append(columnName + ","); strclass2.Append(preParameter + columnName + ","); strclass4.AppendSpaceLine(3, "parameters[" + n + "].Value = model." + columnName + ";"); n++; } if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { nkey = n; strclass3.AppendSpaceLine(5, "new SqlParameter(\"@ReturnValue\",SqlDbType.Int),"); strclass4.AppendSpaceLine(3, "parameters[" + nkey.ToString() + "].Direction = ParameterDirection.Output;"); } //去掉最后的逗号 strclass1.DelLastComma(); strclass2.DelLastComma(); strclass3.DelLastComma(); strclass1.AppendLine(")\");"); strclass.Append(strclass1.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.AppendSpaceLine(3, "strSql.Append(\"" + strclass2.ToString() + ")\");"); if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";set @ReturnValue= @@IDENTITY\");"); } strclass.AppendSpaceLine(3, "" + DbParaHead + "Parameter[] parameters = {"); strclass.Append(strclass3.Value); strclass.AppendLine("};"); strclass.AppendLine(strclass4.Value); #region tran strclass.AppendSpaceLine(3, "List<CommandInfo> sqllist = new List<CommandInfo>();"); strclass.AppendSpaceLine(3, "CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);"); strclass.AppendSpaceLine(3, "sqllist.Add(cmd);"); strclass.AppendSpaceLine(3, "StringBuilder strSql2;"); strclass.AppendSpaceLine(3, "foreach (" + ModelSpaceSon + " models in model." + ModelNameSon + "s)"); strclass.AppendSpaceLine(3, "{"); StringPlus strclass11 = new StringPlus(); StringPlus strclass21 = new StringPlus(); StringPlus strclass31 = new StringPlus(); StringPlus strclass41 = new StringPlus(); //新的增加 strclass.AppendSpaceLine(4, "strSql2=new StringBuilder();"); strclass.AppendSpaceLine(4, "strSql2.Append(\"insert into " + _tablenameson + "(\");"); strclass11.AppendSpace(4, "strSql2.Append(\""); int ns = 0; foreach (ColumnInfo field in FieldlistSon) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; string Length = field.Length; if (field.IsIdentity) { continue; } strclass31.AppendSpaceLine(6, "new " + DbParaHead + "Parameter(\"" + preParameter + columnName + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, columnType, Length) + "),"); strclass11.Append(columnName + ","); strclass21.Append(preParameter + columnName + ","); strclass41.AppendSpaceLine(4, "parameters2[" + ns + "].Value = models." + columnName + ";"); ns++; } strclass11.DelLastComma(); strclass21.DelLastComma(); strclass31.DelLastComma(); strclass11.AppendLine(")\");"); strclass.Append(strclass11.ToString()); strclass.AppendSpaceLine(4, "strSql2.Append(\" values (\");"); strclass.AppendSpaceLine(4, "strSql2.Append(\"" + strclass21.ToString() + ")\");"); //if (IsHasIdentity) //{ // strclass.AppendSpaceLine(4, "strSql2.Append(\";select @@IDENTITY\");"); //} strclass.AppendSpaceLine(4, "" + DbParaHead + "Parameter[] parameters2 = {"); strclass.Append(strclass31.Value); strclass.AppendLine("};"); strclass.AppendLine(strclass41.Value); //end新的增加 strclass.AppendSpaceLine(4, "cmd = new CommandInfo(strSql2.ToString(), parameters2);"); strclass.AppendSpaceLine(4, "sqllist.Add(cmd);"); strclass.AppendSpaceLine(3, "}"); #endregion //重新定义方法头 if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, DbHelperName + ".ExecuteSqlTranWithIndentity(sqllist);"); strclass.AppendSpaceLine(3, "return (" + _keyType + ")parameters[" + nkey + "].Value;"); } else { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSqlTran(sqllist);"); } strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
/// <summary> /// �õ�GetList()�Ĵ��� /// </summary> public string CreatGetList() { StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableNameParent + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); if ((dbobj.DbType == "SQL2000") || (dbobj.DbType == "SQL2005") || (dbobj.DbType == "SQL2008") || (dbobj.DbType == "SQL2012")) { strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList2"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(int Top,string strWhere,string filedOrder)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"select \");"); strclass.AppendSpaceLine(3, "if(Top>0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" top \"+Top.ToString());"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" " + Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableNameParent + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" order by \" + filedOrder);"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); } return strclass.Value; }
/// <summary> /// �õ�Add()�Ĵ��� /// </summary> public string CreatAdd() { StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); StringPlus strclass3 = new StringPlus(); StringPlus strclass4 = new StringPlus(); strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// ����һ������,�����ӱ�����"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") && (IsHasIdentity)) { strretu = "int"; } //��������ͷ string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpaceParent + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + _tablenameparent + "(\");"); strclass1.AppendSpace(3, "strSql.Append(\""); int n = 0; int nkey = 0; foreach (ColumnInfo field in FieldlistParent) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; string Length = field.Length; if (field.IsIdentity) { //nkey = n; continue; } strclass3.AppendSpaceLine(5, "new " + DbParaHead + "Parameter(\"" + preParameter + columnName + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, columnType, Length) + "),"); strclass1.Append(columnName + ","); strclass2.Append(preParameter + columnName + ","); strclass4.AppendSpaceLine(3, "parameters[" + n + "].Value = model." + columnName + ";"); n++; } if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") && (IsHasIdentity)) { nkey = n; strclass3.AppendSpaceLine(5, "new SqlParameter(\"@ReturnValue\",SqlDbType.Int),"); strclass4.AppendSpaceLine(3, "parameters[" + nkey.ToString() + "].Direction = ParameterDirection.Output;"); } //ȥ�����Ķ��� strclass1.DelLastComma(); strclass2.DelLastComma(); strclass3.DelLastComma(); strclass1.AppendLine(")\");"); strclass.Append(strclass1.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.AppendSpaceLine(3, "strSql.Append(\"" + strclass2.ToString() + ")\");"); if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";set @ReturnValue= @@IDENTITY\");"); } strclass.AppendSpaceLine(3, "" + DbParaHead + "Parameter[] parameters = {"); strclass.Append(strclass3.Value); strclass.AppendLine("};"); strclass.AppendLine(strclass4.Value); #region tran strclass.AppendSpaceLine(3, "List<CommandInfo> sqllist = new List<CommandInfo>();"); strclass.AppendSpaceLine(3, "CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);"); strclass.AppendSpaceLine(3, "sqllist.Add(cmd);"); strclass.AppendSpaceLine(3, "StringBuilder strSql2;"); strclass.AppendSpaceLine(3, "foreach (" + ModelSpaceSon + " models in model." + ModelNameSon + "s)"); strclass.AppendSpaceLine(3, "{"); StringPlus strclass11 = new StringPlus(); StringPlus strclass21 = new StringPlus(); StringPlus strclass31 = new StringPlus(); StringPlus strclass41 = new StringPlus(); //�µ����� strclass.AppendSpaceLine(4, "strSql2=new StringBuilder();"); strclass.AppendSpaceLine(4, "strSql2.Append(\"insert into " + _tablenameson + "(\");"); strclass11.AppendSpace(4, "strSql2.Append(\""); int ns = 0; foreach (ColumnInfo field in FieldlistSon) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; string Length = field.Length; if (field.IsIdentity) { continue; } strclass31.AppendSpaceLine(6, "new " + DbParaHead + "Parameter(\"" + preParameter + columnName + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, columnType, Length) + "),"); strclass11.Append(columnName + ","); strclass21.Append(preParameter + columnName + ","); strclass41.AppendSpaceLine(4, "parameters2[" + ns + "].Value = models." + columnName + ";"); ns++; } strclass11.DelLastComma(); strclass21.DelLastComma(); strclass31.DelLastComma(); strclass11.AppendLine(")\");"); strclass.Append(strclass11.ToString()); strclass.AppendSpaceLine(4, "strSql2.Append(\" values (\");"); strclass.AppendSpaceLine(4, "strSql2.Append(\"" + strclass21.ToString() + ")\");"); //if (IsHasIdentity) //{ // strclass.AppendSpaceLine(4, "strSql2.Append(\";select @@IDENTITY\");"); //} strclass.AppendSpaceLine(4, "" + DbParaHead + "Parameter[] parameters2 = {"); strclass.Append(strclass31.Value); strclass.AppendLine("};"); strclass.AppendLine(strclass41.Value); //end�µ����� strclass.AppendSpaceLine(4, "cmd = new CommandInfo(strSql2.ToString(), parameters2);"); strclass.AppendSpaceLine(4, "sqllist.Add(cmd);"); strclass.AppendSpaceLine(3, "}"); #endregion //���¶��巽��ͷ if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, DbHelperName + ".ExecuteSqlTranWithIndentity(sqllist);"); strclass.AppendSpaceLine(3, "return (" + _keyType + ")parameters[" + nkey + "].Value;"); } else { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSqlTran(sqllist);"); } strclass.AppendSpace(2, "}"); return strclass.ToString(); }
/// <summary> /// 生成实体类的属性 /// </summary> /// <returns></returns> public string CreatModelMethod() { StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); strclass.AppendSpaceLine(2, "#region Model"); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnTypedb = field.TypeName; bool IsIdentity = field.IsIdentity; bool ispk = field.IsPrimaryKey; bool cisnull = field.Nullable; //string defValue=field.DefaultVal; string deText = field.Description; string columnType = CodeCommon.DbTypeToCS(columnTypedb); string isnull = ""; if (CodeCommon.isValueType(columnType)) { if ((!IsIdentity) && (!ispk) && (cisnull)) { isnull = "?";//代表可空类型 } } strclass1.AppendSpace(2, "private " + columnType + isnull + " _" + columnName.ToLower());//私有变量 if (field.DefaultVal.Length > 0) { switch (columnType.ToLower()) { case "int": case "long": strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "")); break; case "bool": case "bit": { string val = field.DefaultVal.Trim().Replace("'", "").ToLower(); if (val == "1" || val == "true") { strclass1.Append("= true"); } else { strclass1.Append("= false"); } } break; case "nchar": case "ntext": case "nvarchar": case "char": case "text": case "varchar": case "string": if (field.DefaultVal.Trim().StartsWith("N'")) { strclass1.Append("=" + field.DefaultVal.Trim().Remove(0, 1).Replace("'", "\"")); } else { if (field.DefaultVal.Trim().IndexOf("'") > -1) { strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "\"")); } else { strclass1.Append("= \"" + field.DefaultVal.Trim().Replace("(", "").Replace(")", "") + "\""); } } break; case "datetime": if (field.DefaultVal == "getdate" || field.DefaultVal == "Now()" || field.DefaultVal == "Now" || field.DefaultVal == "CURRENT_TIME" || field.DefaultVal == "CURRENT_DATE" ) { strclass1.Append("= DateTime.Now"); } else { strclass1.Append("= Convert.ToDateTime(" + field.DefaultVal.Trim().Replace("'", "\"") + ")"); } break; case "uniqueidentifier": { //if (field.DefaultVal == "newid") //{ // strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "")); //} } break; case "decimal": case "double": case "float": { strclass1.Append("=" + field.DefaultVal.Replace("'", "").Replace("(", "").Replace(")", "").ToLower() + "M"); } break; //case "sys_guid()": // break; default: // strclass1.Append("=" + field.DefaultVal); break; } } strclass1.AppendLine(";"); strclass2.AppendSpaceLine(2, "/// <summary>"); strclass2.AppendSpaceLine(2, "/// " + deText); strclass2.AppendSpaceLine(2, "/// </summary>"); strclass2.AppendSpaceLine(2, "public " + columnType + isnull + " " + columnName);//属性 strclass2.AppendSpaceLine(2, "{"); strclass2.AppendSpaceLine(3, "set{" + " _" + columnName.ToLower() + "=value;}"); strclass2.AppendSpaceLine(3, "get{return " + "_" + columnName.ToLower() + ";}"); strclass2.AppendSpaceLine(2, "}"); } strclass.Append(strclass1.Value); strclass.Append(strclass2.Value); strclass.AppendSpaceLine(2, "#endregion Model"); return(strclass.ToString()); }
/// <summary> /// �õ�Add()�Ĵ��� /// </summary> public string CreatAdd() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); StringPlus strclass3 = new StringPlus(); StringPlus strclass4 = new StringPlus(); //strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryadd"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "bool"; // strclass.AppendSpaceLine(2, "//" + dbobj.DbType); // strclass.AppendSpaceLine(2, "//" + (dbobj.DbType == "MySQL")); // strclass.AppendSpaceLine(2, "//" + IsHasIdentity); if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012" || dbobj.DbType == "SQLite" || dbobj.DbType == "MySQL") && (IsHasIdentity)) { strretu = "int"; if (_IdentityKeyType != "int") { strretu = _IdentityKeyType; } } //��������ͷ string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "int indexPar = 0;"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + _tablename + "(\");"); strclass1.AppendSpace(3, "strSql.Append(\""); //int n = 0; ��Ϊ�ڲ� indexPar++ foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; string Length = field.Length; bool nullable = field.Nullable; // strclass4.AppendSpaceLine(3, " // " + field.IsIdentity); if (field.IsIdentity || field.ColumnName == "PK_ID") { continue; } strclass3.AppendSpaceLine(5, "new " + DbParaHead + "Parameter(\"" + preParameter + columnName + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, columnType, Length) + "),"); strclass1.Append(columnName + ","); strclass2.Append(preParameter + columnName + ","); if ("uniqueidentifier" == columnType.ToLower()) { strclass4.AppendSpaceLine(3, "parameters[indexPar++].Value = Guid.NewGuid();"); } else { strclass4.AppendSpaceLine(3, "parameters[indexPar++].Value = " + DbHelperName + ".Var2Db(model." + columnName + ");"); } // n++; } //ȥ�����Ķ��� strclass1.DelLastComma(); strclass2.DelLastComma(); strclass3.DelLastComma(); strclass1.AppendLine(")\");"); strclass.Append(strclass1.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.AppendSpaceLine(3, "strSql.Append(\"" + strclass2.ToString() + ")\");"); if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select @@IDENTITY\");"); } if ((dbobj.DbType == "SQLite") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select LAST_INSERT_ROWID()\");"); } if ((dbobj.DbType == "MySQL") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\"; select @@IDENTITY\");"); } strclass.AppendSpaceLine(3, "" + DbParaHead + "Parameter[] parameters = {"); strclass.Append(strclass3.Value); strclass.AppendLine("};"); strclass.AppendLine(strclass4.Value); //���¶��巽��ͷ if (strretu == "void") { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSql(strSql.ToString(),parameters);"); } else if (strretu == "bool") { strclass.AppendSpaceLine(3, "int rows=" + DbHelperName + ".ExecuteSql(strSql.ToString(),parameters);"); strclass.AppendSpaceLine(3, "if (rows > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); } else { strclass.AppendSpaceLine(3, "object obj = " + DbHelperName + ".GetSingle(strSql.ToString(),parameters);"); strclass.AppendSpaceLine(3, "if (obj == null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return 0;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); switch (strretu) { case "int": strclass.AppendSpaceLine(4, "return Convert.ToInt32(obj);"); break; case "long": strclass.AppendSpaceLine(4, "return Convert.ToInt64(obj);"); break; case "decimal": strclass.AppendSpaceLine(4, "return Convert.ToDecimal(obj);"); break; } strclass.AppendSpaceLine(3, "}"); } strclass.AppendSpace(2, "}"); return strclass.ToString(); }
/// <summary> /// �õ�Update�����Ĵ��� /// </summary> /// <param name="DbName"></param> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <param name="ModelName"></param> /// <returns></returns> public string CreatUpdate() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryUpdate"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool Update(" + ModelSpace + " model)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"update " + _tablename + " set \");"); if (Fieldlist.Count == 0) { Fieldlist = Keys; } foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; string Length = field.Length; bool IsIdentity = field.IsIdentity; bool isPK = field.IsPrimaryKey; bool nullable = field.Nullable; if (field.IsIdentity || field.IsPrimaryKey || (Keys.Contains(field))) { continue; } if (columnType.ToLower() == "timestamp") { continue; } strclass.AppendSpaceLine(3, "if (model." + columnName + " != null)"); strclass.AppendSpaceLine(3, "{"); if ((dbobj.DbType == "Oracle") && (columnType.ToLower() == "date" || columnType.ToLower() == "datetime")) { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "=to_date('\" + model." + columnName + ".ToString() + \"','YYYY-MM-DD HH24:MI:SS'),\");"); } else if (columnType.ToLower() == "bit") { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "=\"+ (model." + columnName + "? 1 : 0) +\",\");"); } else if (CodeCommon.IsAddMark(columnType.Trim())) { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "='\"+model." + columnName + "+\"',\");"); } else { strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "=\"+model." + columnName + "+\",\");"); } strclass.AppendSpaceLine(3, "}"); if (nullable) { strclass.AppendSpaceLine(3, "else");//��null����� strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\"" + columnName + "= null ,\");"); strclass.AppendSpaceLine(3, "}"); } } //ȥ�����Ķ��� strclass.AppendSpaceLine(3, "int n = strSql.ToString().LastIndexOf(\",\");"); strclass.AppendSpaceLine(3, "strSql.Remove(n, 1);"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + Maticsoft.CodeHelper.CodeCommon.GetModelWhereExpression(Keys, true) + "\");"); strclass.AppendSpaceLine(3, "int rowsAffected=" + DbHelperName + ".ExecuteSql(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (rowsAffected > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpace(2, "}"); return strclass.ToString(); }
/// <summary> /// ����ʵ��������� /// </summary> /// <returns></returns> public string CreatModelMethod() { StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnTypedb = field.TypeName; bool IsIdentity = field.IsIdentity; bool ispk = field.IsPrimaryKey; bool cisnull = field.Nullable; //string defValue=field.DefaultVal; string deText = field.Description; string columnType = CodeCommon.DbTypeToCS(columnTypedb); string isnull = ""; if (CodeCommon.isValueType(columnType)) { if ((!IsIdentity) && (!ispk) && (cisnull)) { isnull = "?";//����ɿ����� } } strclass1.AppendSpace(2, "private " + columnType + isnull + " _" + columnName.ToLower());//˽�б��� if (field.DefaultVal.Length > 0) { switch (columnType.ToLower()) { case "int": case "long": strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "")); break; case "bool": case "bit": { string val=field.DefaultVal.Trim().Replace("'", "").ToLower(); if(val=="1"||val=="true") { strclass1.Append("= true" ); } else { strclass1.Append("= false"); } } break; case "nchar": case "ntext": case "nvarchar": case "char": case "text": case "varchar": case "string": if (field.DefaultVal.Trim().StartsWith("N'")) { strclass1.Append("=" + field.DefaultVal.Trim().Remove(0, 1).Replace("'", "\"")); } else { if (field.DefaultVal.Trim().IndexOf("'") > -1) { strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "\"")); } else { strclass1.Append("= \"" + field.DefaultVal.Trim().Replace("(", "").Replace(")", "") + "\""); } } break; case "datetime": if (field.DefaultVal == "getdate"|| field.DefaultVal == "Now()"|| field.DefaultVal == "Now"|| field.DefaultVal == "CURRENT_TIME" || field.DefaultVal == "CURRENT_DATE"|| field.DefaultVal.ToLower().Trim((char)13).Trim((char)10) == "sysdate"|| field.DefaultVal.ToUpper().Trim((char)13).Trim((char)10) == "CURRENT_TIMESTAMP" ) { strclass1.Append("= DateTime.Now"); } else { strclass1.Append("= Convert.ToDateTime(" + field.DefaultVal.Trim().Replace("'", "\"") + ")"); } break; case "uniqueidentifier": { //if (field.DefaultVal == "newid") //{ // strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "")); //} } break; case "decimal": case "double": case "float": { // strclass1.Append("=" + field.DefaultVal.Replace("'", "").Replace("(", "").Replace(")", "").ToLower()); //strclass1.Append("=" + field.DefaultVal.Replace("'", "").Replace("(", "").Replace(")", "").ToLower().Replace((char)13, '\0').Replace((char)10, '\0').Trim('\0')); strclass1.Append("=" + field.DefaultVal.Replace("'", "").Replace("(", "").Replace(")", "").ToLower().Trim((char)13).Trim((char)10)+"M"); // ���ȥ�ո�ͻس����� //strclass1.Append(); } break; //case "sys_guid()": // break; default: // strclass1.Append("=" + field.DefaultVal); break; } } strclass1.AppendLine(";"); strclass2.AppendSpaceLine(2, "/// <summary>"); strclass2.AppendSpaceLine(2, "/// " + deText); strclass2.AppendSpaceLine(2, "/// </summary>"); strclass2.AppendSpaceLine(2, "public " + columnType + isnull + " " + columnName);//���� strclass2.AppendSpaceLine(2, "{"); strclass2.AppendSpaceLine(3, "set{" + " _" + columnName.ToLower() + "=value;}"); strclass2.AppendSpaceLine(3, "get{return " + "_" + columnName.ToLower() + ";}"); strclass2.AppendSpaceLine(2, "}"); } strclass.AppendSpaceLine(2, " //�ڲ���Ա����"); strclass.Append(strclass1.Value); strclass.AppendLine(); strclass.AppendSpaceLine(2, "#region ʵ������"); strclass.Append(strclass2.Value); strclass.AppendSpaceLine(2, "#endregion"); return strclass.ToString(); }
/// <summary> /// �õ�Add()�Ĵ��� /// </summary> public string CreatAdd() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryadd"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "bool"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012" || dbobj.DbType == "SQLite") && (IsHasIdentity)) { strretu = "int"; if (_IdentityKeyType != "int") { strretu = _IdentityKeyType; } } //if (dbobj.DbType == "OleDb" && IsHasIdentity) //{ // strretu = "bool"; //} //��������ͷ string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "StringBuilder strSql1=new StringBuilder();"); strclass.AppendSpaceLine(3, "StringBuilder strSql2=new StringBuilder();"); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; if (IsIdentity) { continue; } strclass.AppendSpaceLine(3, "if (model." + columnName + " != null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql1.Append(\"" + columnName + ",\");"); if ((dbobj.DbType == "Oracle") && (columnType.ToLower() == "date" || columnType.ToLower() == "datetime")) { strclass.AppendSpaceLine(4, "strSql2.Append(\"to_date('\" + model." + columnName + ".ToString() + \"','YYYY-MM-DD HH24:MI:SS'),\");"); } else if (columnType.ToLower() == "bit") { strclass.AppendSpaceLine(4, "strSql2.Append(\"\"+(model." + columnName + "? 1 : 0) +\",\");"); } else if ("uniqueidentifier" == columnType.ToLower()) { strclass.AppendSpaceLine(4, "strSql2.Append(\"'\"+ Guid.NewGuid().ToString() +\"',\");"); } else if (CodeCommon.IsAddMark(columnType.Trim())) { strclass.AppendSpaceLine(4, "strSql2.Append(\"'\"+model." + columnName + "+\"',\");"); } else { strclass.AppendSpaceLine(4, "strSql2.Append(\"\"+model." + columnName + "+\",\");"); } strclass.AppendSpaceLine(3, "}"); } strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + TableName + "(\");"); strclass.AppendSpaceLine(3, "strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));"); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.AppendSpaceLine(3, "strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));"); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); if (strretu == "void") { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSql(strSql.ToString());"); } else if (strretu == "bool") { strclass.AppendSpaceLine(3, "int rows=" + DbHelperName + ".ExecuteSql(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (rows > 0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return true;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return false;"); strclass.AppendSpaceLine(3, "}"); } else//�����Զ�������ֵ { if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008" || dbobj.DbType == "SQL2012") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select @@IDENTITY\");"); } if ((dbobj.DbType == "SQLite") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select LAST_INSERT_ROWID()\");"); } strclass.AppendSpaceLine(3, "object obj = " + DbHelperName + ".GetSingle(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (obj == null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return 0;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); switch (strretu) { case "int": strclass.AppendSpaceLine(4, "return Convert.ToInt32(obj);"); break; case "long": strclass.AppendSpaceLine(4, "return Convert.ToInt64(obj);"); break; case "decimal": strclass.AppendSpaceLine(4, "return Convert.ToDecimal(obj);"); break; } strclass.AppendSpaceLine(3, "}"); } strclass.AppendSpace(2, "}"); return strclass.ToString(); }
public string GetDALCode(bool Maxid, bool Exists, bool Add, bool Update, bool Delete, bool GetModel, bool List) { StringPlus stringPlus = new StringPlus(); stringPlus.AppendLine("using System;"); stringPlus.AppendLine("using System.Data;"); stringPlus.AppendLine("using System.Text;"); string dbType; switch (dbType = this.dbobj.DbType) { case "SQL2005": case "SQL2008": case "SQL2012": stringPlus.AppendLine("using System.Data.SqlClient;"); break; case "SQL2000": stringPlus.AppendLine("using System.Data.SqlClient;"); break; case "Oracle": stringPlus.AppendLine("using System.Data.OracleClient;"); break; case "MySQL": stringPlus.AppendLine("using MySql.Data.MySqlClient;"); break; case "OleDb": stringPlus.AppendLine("using System.Data.OleDb;"); break; case "SQLite": stringPlus.AppendLine("using System.Data.SQLite;"); break; } if (this.IDALpath != "") { stringPlus.AppendLine("using " + this.IDALpath + ";"); } stringPlus.AppendLine("using Maticsoft.DBUtility;//Please add references"); stringPlus.AppendLine("namespace " + this.DALpath); stringPlus.AppendLine("{"); stringPlus.AppendSpaceLine(1, "/// <summary>"); stringPlus.AppendSpaceLine(1, "/// " + this.Languagelist["summary"].ToString() + ":" + this.DALName); stringPlus.AppendSpaceLine(1, "/// </summary>"); stringPlus.AppendSpace(1, "public partial class " + this.DALName); if (this.IClass != "") { stringPlus.Append(":" + this.IClass); } stringPlus.AppendLine(""); stringPlus.AppendSpaceLine(1, "{"); stringPlus.AppendSpaceLine(2, "public " + this.DALName + "()"); stringPlus.AppendSpaceLine(2, "{}"); stringPlus.AppendSpaceLine(2, "#region Method"); if (Maxid) { stringPlus.AppendLine(this.CreatGetMaxID()); } if (Exists) { stringPlus.AppendLine(this.CreatExists()); } if (Add) { stringPlus.AppendLine(this.CreatAdd()); } if (Update) { stringPlus.AppendLine(this.CreatUpdate()); } if (Delete) { stringPlus.AppendLine(this.CreatDelete()); } if (GetModel) { stringPlus.AppendLine(this.CreatGetModel()); stringPlus.AppendLine(this.CreatDataRowToModel()); } if (List) { stringPlus.AppendLine(this.CreatGetList()); stringPlus.AppendLine(this.CreatGetListByPage()); stringPlus.AppendLine(this.CreatGetListByPageProc()); } stringPlus.AppendSpaceLine(2, "#endregion Method"); stringPlus.AppendSpaceLine(2, "#region MethodEx"); stringPlus.AppendLine(""); stringPlus.AppendSpaceLine(2, "#endregion MethodEx"); stringPlus.AppendSpaceLine(1, "}"); stringPlus.AppendLine("}"); stringPlus.AppendLine(""); return(stringPlus.ToString()); }
/// <summary> /// �õ�Exists�����Ĵ��� /// </summary> /// <param name="_tablename"></param> /// <param name="ID"></param> /// <returns></returns> public string CreatExists() { StringPlus strclass = new StringPlus(); if (_keys.Count > 0) { string strInparam = Maticsoft.CodeHelper.CodeCommon.GetInParameter(Keys, false); if (!string.IsNullOrEmpty(strInparam)) { strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryExists"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public bool Exists(" + strInparam + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"select count(1) from " + _tablename + "\");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + Maticsoft.CodeHelper.CodeCommon.GetWhereExpression(Keys, false) + "\");"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Exists(strSql.ToString());"); strclass.AppendSpace(2, "}"); } } return strclass.ToString(); }
/// <summary> /// 得到整个类的代码 /// </summary> public string GetDALCode(bool Maxid, bool Exists, bool Add, bool Update, bool Delete, bool GetModel, bool List) { StringPlus strclass = new StringPlus(); strclass.AppendLine("using System;"); strclass.AppendLine("using System.Data;"); strclass.AppendLine("using System.Text;"); switch (dbobj.DbType) { case "SQL2005": strclass.AppendLine("using System.Data.SqlClient;"); break; case "SQL2000": strclass.AppendLine("using System.Data.SqlClient;"); break; case "Oracle": strclass.AppendLine("using System.Data.OracleClient;"); break; case "MySQL": strclass.AppendLine("using MySql.Data.MySqlClient;"); break; case "OleDb": strclass.AppendLine("using System.Data.OleDb;"); break; } if (IDALpath != "") { strclass.AppendLine("using " + IDALpath + ";"); } strclass.AppendLine("using Maticsoft.DBUtility;//请先添加引用"); strclass.AppendLine("namespace " + DALpath); strclass.AppendLine("{"); strclass.AppendSpaceLine(1, "/// <summary>"); strclass.AppendSpaceLine(1, "/// 数据访问类" + DALName + "。"); strclass.AppendSpaceLine(1, "/// </summary>"); strclass.AppendSpace(1, "public class " + DALName); if (IClass != "") { strclass.Append(":" + IClass); } strclass.AppendLine(""); strclass.AppendSpaceLine(1, "{"); strclass.AppendSpaceLine(2, "public " + DALName + "()"); strclass.AppendSpaceLine(2, "{}"); strclass.AppendSpaceLine(2, "#region 成员方法"); #region 方法代码 if (Maxid) { strclass.AppendLine(CreatGetMaxID()); } if (Exists) { strclass.AppendLine(CreatExists()); } if (Add) { strclass.AppendLine(CreatAdd()); } if (Update) { strclass.AppendLine(CreatUpdate()); } if (Delete) { strclass.AppendLine(CreatDelete()); } if (GetModel) { strclass.AppendLine(CreatGetModel()); } if (List) { strclass.AppendLine(CreatGetList()); strclass.AppendLine(CreatGetListByPageProc()); } #endregion strclass.AppendSpaceLine(2, "#endregion 成员方法"); strclass.AppendSpaceLine(1, "}"); strclass.AppendLine("}"); strclass.AppendLine(""); return(strclass.ToString()); }
public static string GetTableScript(DataBaseType currDBType, TableInfo tableInfo, DataBaseType tgtDBType) { StringPlus sPlus = new StringPlus(); var lstCols = tableInfo.LstColInfo; if (currDBType == DataBaseType.MsSql && tgtDBType == DataBaseType.MsSql) { #region CREATE TABLE By MsSql sPlus.AppendLine("CREATE TABLE [" + tableInfo.TableName + "]"); sPlus.AppendLine("("); List <string> lstDefSpt = new List <string>(); List <string> lstComments = new List <string>(); for (int j = 0; j < lstCols.Count; j++) { ColumnInfo col = lstCols[j]; sPlus.AppendSpace(2, "[" + col.ColumnName + "] " + col.NewTypeName(currDBType) + ""); if (col.IsIdentity) { sPlus.Append(" identity(1,1) "); } if (col.CanNull) { sPlus.Append(" null "); } else { sPlus.Append(" not null "); } if (col.IsPK) { sPlus.Append(" primary key "); } if (j != (lstCols.Count - 1)) { sPlus.AppendLine(","); } else { sPlus.AppendLine(); } if (!string.IsNullOrEmpty(col.DefaultVal)) { string def_spt = "ALTER TABLE [" + tableInfo.TableName + "] ADD DEFAULT " + col.DefaultVal + " FOR [" + col.ColumnName + "]"; lstDefSpt.Add(def_spt); } if (!string.IsNullOrWhiteSpace(col.DeText)) { string desc_spt = "EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'" + col.DeText + "' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'" + tableInfo.TableName + "', @level2type=N'COLUMN',@level2name=N'" + col.ColumnName + "'"; lstComments.Add(desc_spt); } } sPlus.AppendLine(") ON [PRIMARY] "); sPlus.AppendLine("GO"); if (lstDefSpt.Count > 0) { string def_script = string.Join("\r\nGO\r\n", lstDefSpt.ToArray()); sPlus.AppendLine(def_script); sPlus.AppendLine("GO"); } if (!string.IsNullOrEmpty(tableInfo.TabComment)) { string tab_comment_spt = @"EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'" + tableInfo.TabComment + "' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'" + tableInfo.TableName + "'"; sPlus.AppendLine(tab_comment_spt); sPlus.AppendLine("GO"); } if (lstComments.Count > 0) { string col_comment_spt = string.Join("\r\nGO\r\n", lstComments.ToArray()); sPlus.AppendLine(col_comment_spt); sPlus.Append("GO"); } #endregion } else if (currDBType == DataBaseType.Oracle && tgtDBType == DataBaseType.Oracle) { #region CREATE TABLE By MsSql sPlus.AppendLine("CREATE TABLE " + tableInfo.TableName + ""); sPlus.AppendLine("("); List <string> lstDefSpt = new List <string>(); List <string> lstComments = new List <string>(); for (int j = 0; j < lstCols.Count; j++) { ColumnInfo col = lstCols[j]; sPlus.AppendSpace(2, col.ColumnName + " " + col.NewTypeName(currDBType) + ""); if (col.CanNull) { sPlus.Append(" null "); } else { sPlus.Append(" not null "); } if (col.IsPK) { sPlus.Append(" primary key "); } if (j != (lstCols.Count - 1)) { sPlus.AppendLine(","); } else { sPlus.AppendLine(); } //if (!string.IsNullOrEmpty(col.DefaultVal)) //{ // string def_spt = "ALTER TABLE [" + tableInfo.TableName + "] ADD DEFAULT " + col.DefaultVal + " FOR [" + col.ColumnName + "]"; // lstDefSpt.Add(def_spt); //} if (!string.IsNullOrWhiteSpace(col.DeText)) { string desc_spt = "COMMENT ON TABLE " + tableInfo.TableName + "." + col.ColumnName + " IS '" + col.DeText + "'"; lstComments.Add(desc_spt); } //if (col.IsIdentity) //{ // sPlus.Append(" identity(1,1) "); //} } sPlus.AppendLine(")"); sPlus.AppendLine("LOGGING"); sPlus.AppendLine("NOCOMPRESS"); sPlus.AppendLine("NOCACHE"); sPlus.AppendLine(";"); //if (lstDefSpt.Count > 0) //{ // string def_script = string.Join("\r\nGO\r\n", lstDefSpt.ToArray()); // sPlus.AppendLine(def_script); // sPlus.AppendLine("GO"); //} if (!string.IsNullOrEmpty(tableInfo.TabComment)) { string tab_comment_spt = @"COMMENT ON TABLE " + tableInfo.TableName + " IS '" + tableInfo.TabComment + "';"; sPlus.AppendLine(tab_comment_spt); } if (lstComments.Count > 0) { string col_comment_spt = string.Join("\r\n", lstComments.ToArray()); sPlus.AppendLine(col_comment_spt); } #endregion } else if (currDBType == DataBaseType.MySql && tgtDBType == DataBaseType.MySql) { } else if (currDBType == DataBaseType.Sqlite && tgtDBType == DataBaseType.Sqlite) { } return(sPlus.Value); }
/// <summary> /// 得到GetModel()的代码 /// </summary> /// <param name="DbName"></param> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <param name="ModelName"></param> /// <returns></returns> public string CreatGetModel() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 得到一个对象实体"); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public " + ModelSpace + " GetModel(" + LTP.CodeHelper.CodeCommon.GetInParameter(Keys) + ")"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, KeysNullTip); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); if (dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2008") { strclass.Append(" top 1 "); } strclass.AppendLine(Fieldstrlist + " from " + _tablename + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" where " + GetWhereExpression(Keys) + "\");"); strclass.AppendLine(GetPreParameter(Keys)); strclass.AppendSpaceLine(3, "" + ModelSpace + " model=new " + ModelSpace + "();"); strclass.AppendSpaceLine(3, "DataSet ds=" + DbHelperName + ".Query(strSql.ToString(),parameters);"); strclass.AppendSpaceLine(3, "if(ds.Tables[0].Rows.Count>0)"); strclass.AppendSpaceLine(3, "{"); #region 字段赋值 foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; switch (CodeCommon.DbTypeToCS(columnType)) { case "int": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=int.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); strclass.AppendSpaceLine(4, "}"); } break; case "long": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=long.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); strclass.AppendSpaceLine(4, "}"); } break; case "decimal": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=decimal.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); strclass.AppendSpaceLine(4, "}"); } break; case "float": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=float.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); strclass.AppendSpaceLine(4, "}"); } break; case "DateTime": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=DateTime.Parse(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); strclass.AppendSpaceLine(4, "}"); } break; case "string": { strclass.AppendSpaceLine(4, "model." + columnName + "=ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString();"); } break; case "bool": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "if((ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()==\"1\")||(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString().ToLower()==\"true\"))"); strclass.AppendSpaceLine(5, "{"); strclass.AppendSpaceLine(6, "model." + columnName + "=true;"); strclass.AppendSpaceLine(5, "}"); strclass.AppendSpaceLine(5, "else"); strclass.AppendSpaceLine(5, "{"); strclass.AppendSpaceLine(6, "model." + columnName + "=false;"); strclass.AppendSpaceLine(5, "}"); strclass.AppendSpaceLine(4, "}"); } break; case "byte[]": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=(byte[])ds.Tables[0].Rows[0][\"" + columnName + "\"];"); strclass.AppendSpaceLine(4, "}"); } break; case "Guid": { strclass.AppendSpaceLine(4, "if(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString()!=\"\")"); strclass.AppendSpaceLine(4, "{"); strclass.AppendSpaceLine(5, "model." + columnName + "=new Guid(ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString());"); strclass.AppendSpaceLine(4, "}"); } break; default: strclass.AppendSpaceLine(4, "//model." + columnName + "=ds.Tables[0].Rows[0][\"" + columnName + "\"].ToString();"); break; } } #endregion strclass.AppendSpaceLine(4, "return model;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return null;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(2, "}"); return(strclass.ToString()); }
public string CreatAdd(string tabName, string ModelName, List <ColumnInfo> Fieldlist, int num) { StringPlus stringPlus = new StringPlus(); StringPlus stringPlus2 = new StringPlus(); StringPlus stringPlus3 = new StringPlus(); StringPlus stringPlus4 = new StringPlus(); StringPlus stringPlus5 = new StringPlus(); stringPlus.AppendSpaceLine(3, "StringBuilder strSql" + num + "=new StringBuilder();"); stringPlus.AppendSpaceLine(3, string.Concat(new object[] { "strSql", num, ".Append(\"insert into ", tabName, "(\");" })); stringPlus2.AppendSpace(3, "strSql" + num + ".Append(\""); int num2 = 0; foreach (ColumnInfo current in Fieldlist) { string columnName = current.ColumnName; string typeName = current.TypeName; bool arg_CF_0 = current.IsIdentity; string length = current.Length; bool arg_E0_0 = current.Nullable; if (!current.IsIdentity) { stringPlus4.AppendSpaceLine(5, string.Concat(new string[] { "new ", this.DbParaHead, "Parameter(\"", this.preParameter, columnName, "\", ", this.DbParaDbType, ".", CodeCommon.DbTypeLength(this.dbobj.DbType, typeName, length), ")," })); stringPlus2.Append(columnName + ","); stringPlus3.Append(this.preParameter + columnName + ","); if ("uniqueidentifier" == typeName.ToLower()) { stringPlus5.AppendSpaceLine(3, string.Concat(new object[] { "parameters", num, "[", num2, "].Value = Guid.NewGuid();" })); } else { stringPlus5.AppendSpaceLine(3, string.Concat(new object[] { "parameters", num, "[", num2, "].Value = model", ModelName, ".", columnName, ";" })); } num2++; } } stringPlus2.DelLastComma(); stringPlus3.DelLastComma(); stringPlus4.DelLastComma(); stringPlus2.AppendLine(")\");"); stringPlus.Append(stringPlus2.ToString()); stringPlus.AppendSpaceLine(3, "strSql.Append(\" values (\");"); stringPlus.AppendSpaceLine(3, "strSql.Append(\"" + stringPlus3.ToString() + ")\");"); stringPlus.AppendSpaceLine(3, string.Concat(new object[] { this.DbParaHead, "Parameter[] parameters", num, " = {" })); stringPlus.Append(stringPlus4.Value); stringPlus.AppendLine("};"); stringPlus.AppendLine(stringPlus5.Value); return(stringPlus.ToString()); }
/// <summary> /// 得到Add()的代码 /// </summary> public string CreatAdd() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 增加一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strretu = "int"; } //方法定义头 string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "StringBuilder strSql1=new StringBuilder();"); strclass.AppendSpaceLine(3, "StringBuilder strSql2=new StringBuilder();"); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; if (IsIdentity) { continue; } strclass.AppendSpaceLine(3, "if (model." + columnName + " != null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql1.Append(\"" + columnName + ",\");"); if ((dbobj.DbType == "Oracle") && (columnType.ToLower() == "date" || columnType.ToLower() == "datetime")) { strclass.AppendSpaceLine(4, "strSql2.Append(\"to_date('\" + model." + columnName + ".ToString() + \"','YYYY-MM-DD HH24:MI:SS'),\");"); } else if (columnType.ToLower() == "bit") { strclass.AppendSpaceLine(4, "strSql2.Append(\"\"+(model." + columnName + "? 1 : 0) +\",\");"); } else if (CodeCommon.IsAddMark(columnType.Trim())) { strclass.AppendSpaceLine(4, "strSql2.Append(\"'\"+model." + columnName + "+\"',\");"); } else { strclass.AppendSpaceLine(4, "strSql2.Append(\"\"+model." + columnName + "+\",\");"); } strclass.AppendSpaceLine(3, "}"); } strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + TableName + "(\");"); strclass.AppendSpaceLine(3, "strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));"); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.AppendSpaceLine(3, "strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));"); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); //返回自动增长列值 if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select @@IDENTITY\");"); strclass.AppendSpaceLine(3, "object obj = " + DbHelperName + ".GetSingle(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (obj == null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return 1;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return Convert.ToInt32(obj);"); strclass.AppendSpaceLine(3, "}"); } else { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSql(strSql.ToString());"); } strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
/// <summary> /// 生成实体类的属性 /// </summary> /// <returns></returns> public string CreatModelMethod() { StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); strclass.AppendSpaceLine(2, "#region Model"); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnTypedb = field.TypeName; bool IsIdentity = field.IsIdentity; bool ispk = field.IsPrimaryKey; bool cisnull = field.Nullable; //string defValue=field.DefaultVal; string deText = field.Description; string dbLength = ""; string columnType = CodeCommon.DbTypeToCS(columnTypedb); string isnull = ""; if (CodeCommon.isValueType(columnType)) { if ((!IsIdentity) && (!ispk) && (cisnull)) { isnull = "?";//代表可空类型 } } strclass1.AppendSpace(2, "private " + columnType + isnull + " _" + columnName.ToLower()); //私有变量 if (field.Length.Length > 0) //字符串类型长度 { switch (columnTypedb.ToLower()) { case "nchar": case "ntext": case "nvarchar": case "char": case "text": case "varchar": case "string": case "binary": if (field.Length == "0") { dbLength = "(MAX)"; } else { dbLength = "(" + field.Length.ToString() + ")"; } break; } } if (field.DefaultVal.Length > 0)//默认值 { switch (columnType.ToLower()) { case "int": case "long": strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "")); break; case "bool": case "bit": { string val = field.DefaultVal.Trim().Replace("'", "").ToLower(); if (val == "1" || val == "true") { strclass1.Append("= true"); } else { strclass1.Append("= false"); } } break; case "nchar": case "ntext": case "nvarchar": case "char": case "text": case "varchar": case "string": if (field.DefaultVal.Trim().StartsWith("N'")) { strclass1.Append("=" + field.DefaultVal.Trim().Remove(0, 1).Replace("'", "\"")); } else { if (field.DefaultVal.Trim().IndexOf("'") > -1) { strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "\"")); } else { strclass1.Append("= \"" + field.DefaultVal.Trim().Replace("(", "").Replace(")", "") + "\""); } } break; case "datetime": if (field.DefaultVal == "getdate" || field.DefaultVal == "Now()" || field.DefaultVal == "Now" || field.DefaultVal == "CURRENT_TIME" || field.DefaultVal == "CURRENT_DATE" ) { strclass1.Append("= DateTime.Now"); } else { strclass1.Append("= Convert.ToDateTime(" + field.DefaultVal.Trim().Replace("'", "\"") + ")"); } break; case "uniqueidentifier": { //if (field.DefaultVal == "newid") //{ // strclass1.Append("=" + field.DefaultVal.Trim().Replace("'", "")); //} } break; case "decimal": case "double": case "float": { strclass1.Append("=" + field.DefaultVal.Replace("'", "").Replace("(", "").Replace(")", "").ToLower() + "M"); } break; //case "sys_guid()": // break; default: // strclass1.Append("=" + field.DefaultVal); break; } } strclass1.AppendLine(";"); strclass2.AppendSpaceLine(2, "/// <summary>"); strclass2.AppendSpaceLine(2, "/// " + deText); strclass2.AppendSpaceLine(2, "/// </summary>"); if (ispk)//Linq to sql 的主键映射 { if (deText.Length <= 0) { strclass2.AppendSpaceLine(2, "[Display(Name = \"" + columnName + "\")]"); } else { strclass2.AppendSpaceLine(2, "[Display(Name = \"" + deText + "\")]"); } strclass2.AppendSpaceLine(2, "[Required]"); strclass2.AppendSpaceLine(2, "[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = \"_" + columnName.ToLower() + "\", AutoSync = AutoSync.Always, DbType = \"" + columnTypedb + " NOT NULL IDENTITY\", IsDbGenerated = true)]"); } else { if (deText.Length <= 0) { strclass2.AppendSpaceLine(2, "[Display(Name = \"" + columnName + "\")]"); strclass2.AppendSpaceLine(2, "[Required(ErrorMessage = \"" + columnName + "不能为空\")]"); } else { strclass2.AppendSpaceLine(2, "[Display(Name = \"" + deText + "\")]"); strclass2.AppendSpaceLine(2, "[Required(ErrorMessage = \"" + deText + "不能为空\")]"); } if (cisnull) { strclass2.AppendSpaceLine(2, "[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = \"_" + columnName.ToLower() + "\",DbType = \"" + columnTypedb + dbLength + "\")]"); } else { strclass2.AppendSpaceLine(2, "[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = \"_" + columnName.ToLower() + "\",DbType = \"" + columnTypedb + dbLength + " NOT NULL\")]"); } } strclass2.AppendSpaceLine(2, "public " + columnType + isnull + " " + columnName);//属性 strclass2.AppendSpaceLine(2, "{"); strclass2.AppendSpaceLine(3, "set{" + " _" + columnName.ToLower() + "=value;}"); strclass2.AppendSpaceLine(3, "get{return " + "_" + columnName.ToLower() + ";}"); strclass2.AppendSpaceLine(2, "}"); } strclass.Append(strclass1.Value); //strclass.Append("private List<article_albums> _albums;"); strclass.Append(strclass2.Value); //strclass.Append(" /// <summary>"); //strclass.Append("/// 图片相册列表"); //strclass.Append("/// </summary>"); //strclass.Append("public List<article_albums> albums"); //strclass.Append("{"); //strclass.Append("set { _albums = value; }"); //strclass.Append("get { return _albums; }"); //strclass.Append(" }"); strclass.AppendSpaceLine(2, "#endregion Model"); strclass.AppendSpaceLine(2, "#region INotifyPropertyChanging 成员"); strclass.AppendSpaceLine(2, "public event PropertyChangingEventHandler PropertyChanging;"); strclass.AppendSpaceLine(2, "#endregion"); strclass.AppendSpaceLine(2, "#region INotifyPropertyChanged 成员"); strclass.AppendSpaceLine(2, " public event PropertyChangedEventHandler PropertyChanged;"); strclass.AppendSpaceLine(2, "#endregion"); //strclass.AppendSpaceLine(2, "#region IEnumerator 成员"); //strclass.AppendSpaceLine(2, " public IEnumerator GetEnumerator()"); //strclass.AppendSpaceLine(2, "{"); //strclass.AppendSpaceLine(2, " throw new NotImplementedException();"); //strclass.AppendSpaceLine(2, "}"); //strclass.AppendSpaceLine(2, "#endregion"); return(strclass.ToString()); }
public string GetDALCode() { StringPlus stringPlus = new StringPlus(); stringPlus.AppendLine("using System;"); stringPlus.AppendLine("using System.Data;"); stringPlus.AppendLine("using System.Text;"); stringPlus.AppendLine("using System.Collections.Generic;"); string dbType; switch (dbType = this.dbobj.DbType) { case "SQL2005": case "SQL2008": case "SQL2012": stringPlus.AppendLine("using System.Data.SqlClient;"); break; case "SQL2000": stringPlus.AppendLine("using System.Data.SqlClient;"); break; case "Oracle": stringPlus.AppendLine("using System.Data.OracleClient;"); break; case "OleDb": stringPlus.AppendLine("using System.Data.OleDb;"); break; case "SQLite": stringPlus.AppendLine("using System.Data.SQLite;"); break; } if (this.IDALpath != "") { stringPlus.AppendLine("using " + this.IDALpath + ";"); } stringPlus.AppendLine("using Maticsoft.DBUtility;//Please add references"); stringPlus.AppendLine("namespace " + this.DALpath); stringPlus.AppendLine("{"); stringPlus.AppendSpaceLine(1, "/// <summary>"); stringPlus.AppendSpaceLine(1, "/// " + this.Languagelist["summary"].ToString()); stringPlus.AppendSpaceLine(1, "/// </summary>"); stringPlus.AppendSpace(1, "public partial class Class1"); if (this.IClass != "") { stringPlus.Append(":" + this.IClass); } stringPlus.AppendLine(""); stringPlus.AppendSpaceLine(1, "{"); if (this.ModelTranList.Count == 0) { return(""); } StringPlus stringPlus2 = new StringPlus(); foreach (ModelTran current in this.ModelTranList) { if (current.Action.ToLower() == "delete") { using (List <ColumnInfo> .Enumerator enumerator2 = current.Keys.GetEnumerator()) { while (enumerator2.MoveNext()) { ColumnInfo current2 = enumerator2.Current; if (current2.IsPrimaryKey || !current2.IsIdentity) { stringPlus2.Append(CodeCommon.DbTypeToCS(current2.TypeName) + " " + current2.ColumnName + ","); } } continue; } } stringPlus2.Append(this.ModelSpace(current.ModelName) + " model" + current.ModelName + ","); } stringPlus2.DelLastComma(); stringPlus.AppendSpaceLine(2, "public void TranMethod(" + stringPlus2.Value + " )"); stringPlus.AppendSpaceLine(2, "{"); stringPlus.AppendSpaceLine(3, "List<CommandInfo> cmdlist = new List<CommandInfo>();"); stringPlus.AppendLine(); int num2 = 1; foreach (ModelTran current3 in this.ModelTranList) { string a; if ((a = current3.Action.ToLower()) != null) { if (!(a == "insert")) { if (!(a == "update")) { if (a == "delete") { stringPlus.Append(this.CreatDelete(current3.TableName, current3.ModelName, current3.Fieldlist, current3.Keys, num2)); stringPlus.AppendSpaceLine(3, string.Concat(new object[] { "CommandInfo cmd", num2, " = new CommandInfo(strSql", num2, ".ToString(), parameters", num2, ");" })); stringPlus.AppendSpaceLine(3, "cmdlist.Add(cmd" + num2 + ");"); stringPlus.AppendLine(); } } else { stringPlus.Append(this.CreatUpdate(current3.TableName, current3.ModelName, current3.Fieldlist, current3.Keys, num2)); stringPlus.AppendSpaceLine(3, string.Concat(new object[] { "CommandInfo cmd", num2, " = new CommandInfo(strSql", num2, ".ToString(), parameters", num2, ");" })); stringPlus.AppendSpaceLine(3, "cmdlist.Add(cmd" + num2 + ");"); stringPlus.AppendLine(); } } else { stringPlus.Append(this.CreatAdd(current3.TableName, current3.ModelName, current3.Fieldlist, num2)); stringPlus.AppendSpaceLine(3, string.Concat(new object[] { "CommandInfo cmd", num2, " = new CommandInfo(strSql", num2, ".ToString(), parameters", num2, ");" })); stringPlus.AppendSpaceLine(3, "cmdlist.Add(cmd" + num2 + ");"); stringPlus.AppendLine(); } } num2++; } stringPlus.AppendSpaceLine(3, "DbHelperSQL.ExecuteSqlTran(cmdlist);"); stringPlus.AppendSpaceLine(2, "}"); stringPlus.AppendSpaceLine(1, "}"); stringPlus.AppendLine("}"); stringPlus.AppendLine(""); return(stringPlus.ToString()); }
/// <summary> /// 得到Add()的代码 /// </summary> public string CreatAdd() { if (ModelSpace == "") { //ModelSpace = "ModelClassName"; ; } StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); StringPlus strclass3 = new StringPlus(); StringPlus strclass4 = new StringPlus(); strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 增加一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strretu = "int"; } //方法定义头 string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + _tablename + "(\");"); strclass1.AppendSpace(3, "strSql.Append(\""); int n = 0; foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; string Length = field.Length; if (field.IsIdentity) { continue; } strclass3.AppendSpaceLine(5, "new " + DbParaHead + "Parameter(\"" + preParameter + columnName + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, columnType, Length) + "),"); strclass1.Append(columnName + ","); strclass2.Append(preParameter + columnName + ","); strclass4.AppendSpaceLine(3, "parameters[" + n + "].Value = model." + columnName + ";"); n++; } //去掉最后的逗号 strclass1.DelLastComma(); strclass2.DelLastComma(); strclass3.DelLastComma(); strclass1.AppendLine(")\");"); strclass.Append(strclass1.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.AppendSpaceLine(3, "strSql.Append(\"" + strclass2.ToString() + ")\");"); if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select @@IDENTITY\");"); } strclass.AppendSpaceLine(3, "" + DbParaHead + "Parameter[] parameters = {"); strclass.Append(strclass3.Value); strclass.AppendLine("};"); strclass.AppendLine(strclass4.Value); //重新定义方法头 if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005" || dbobj.DbType == "SQL2008") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "object obj = " + DbHelperName + ".GetSingle(strSql.ToString(),parameters);"); strclass.AppendSpaceLine(3, "if (obj == null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return 1;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return Convert.ToInt32(obj);"); strclass.AppendSpaceLine(3, "}"); } else { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSql(strSql.ToString(),parameters);"); } strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
public string CreatModelMethod() { StringPlus stringPlus = new StringPlus(); StringPlus stringPlus2 = new StringPlus(); StringPlus stringPlus3 = new StringPlus(); stringPlus.AppendSpaceLine(2, "#region Model"); foreach (ColumnInfo current in this.Fieldlist) { string columnName = current.ColumnName; string typeName = current.TypeName; bool isIdentity = current.IsIdentity; bool isPrimaryKey = current.IsPrimaryKey; bool nullable = current.Nullable; string description = current.Description; string text = CodeCommon.DbTypeToCS(typeName); string text2 = ""; if (CodeCommon.isValueType(text) && !isIdentity && !isPrimaryKey && nullable) { text2 = "?"; } stringPlus2.AppendSpace(2, string.Concat(new string[] { "private ", text, text2, " _", columnName.ToLower() })); string key; if (current.DefaultVal.Length > 0 && (key = text.ToLower()) != null) { if (dtCollection == null) { dtCollection = new Dictionary <string, int>(16) { { "int", 0 }, { "long", 1 }, { "bool", 2 }, { "bit", 3 }, { "nchar", 4 }, { "ntext", 5 }, { "nvarchar", 6 }, { "char", 7 }, { "text", 8 }, { "varchar", 9 }, { "string", 10 }, { "datetime", 11 }, { "uniqueidentifier", 12 }, { "decimal", 13 }, { "double", 14 }, { "float", 15 } }; } int num; if (dtCollection.TryGetValue(key, out num)) { switch (num) { case 0: case 1: stringPlus2.Append("=" + current.DefaultVal.Trim().Replace("'", "")); break; case 2: case 3: { string a = current.DefaultVal.Trim().Replace("'", "").ToLower(); if (a == "1" || a == "true") { stringPlus2.Append("= true"); } else { stringPlus2.Append("= false"); } break; } case 4: case 5: case 6: case 7: case 8: case 9: case 10: if (current.DefaultVal.Trim().StartsWith("N'")) { stringPlus2.Append("=" + current.DefaultVal.Trim().Remove(0, 1).Replace("'", "\"")); } else { if (current.DefaultVal.Trim().IndexOf("'") > -1) { stringPlus2.Append("=" + current.DefaultVal.Trim().Replace("'", "\"")); } else { stringPlus2.Append("= \"" + current.DefaultVal.Trim().Replace("(", "").Replace(")", "") + "\""); } } break; case 11: if (current.DefaultVal == "getdate" || current.DefaultVal == "Now()" || current.DefaultVal == "Now" || current.DefaultVal == "CURRENT_TIME" || current.DefaultVal == "CURRENT_DATE") { stringPlus2.Append("= DateTime.Now"); } else { stringPlus2.Append("= Convert.ToDateTime(" + current.DefaultVal.Trim().Replace("'", "\"") + ")"); } break; case 13: case 14: case 15: stringPlus2.Append("=" + current.DefaultVal.Replace("'", "").Replace("(", "").Replace(")", "").ToLower() + "M"); break; } } } stringPlus2.AppendLine(";"); stringPlus3.AppendSpaceLine(2, "/// <summary>"); stringPlus3.AppendSpaceLine(2, "/// " + description.Replace("\r\n", "\r\n\t///")); stringPlus3.AppendSpaceLine(2, "/// </summary>"); stringPlus3.AppendSpaceLine(2, string.Concat(new string[] { "public ", text, text2, " ", columnName })); stringPlus3.AppendSpaceLine(2, "{"); stringPlus3.AppendSpaceLine(3, "set{ _" + columnName.ToLower() + "=value;}"); stringPlus3.AppendSpaceLine(3, "get{return _" + columnName.ToLower() + ";}"); stringPlus3.AppendSpaceLine(2, "}"); } stringPlus.Append(stringPlus2.Value); stringPlus.Append(stringPlus3.Value); stringPlus.AppendSpaceLine(2, "#endregion Model"); return(stringPlus.ToString()); }
/// <summary> /// 得到Add()的代码 /// </summary> public string CreatAdd() { if (ModelSpace == "") { ModelSpace = "ModelClassName";; } StringPlus strclass = new StringPlus(); StringPlus strclass1 = new StringPlus(); StringPlus strclass2 = new StringPlus(); strclass.AppendLine(""); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// 增加一条数据"); strclass.AppendSpaceLine(2, "/// </summary>"); string strretu = "void"; if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005") && (IsHasIdentity)) { strretu = "int"; } //方法定义头 string strFun = CodeCommon.Space(2) + "public " + strretu + " Add(" + ModelSpace + " model)"; strclass.AppendLine(strFun); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"insert into " + _tablename + "(\");"); strclass1.AppendSpace(3, "strSql.Append(\""); foreach (ColumnInfo field in Fieldlist) { string columnName = field.ColumnName; string columnType = field.TypeName; bool IsIdentity = field.IsIdentity; if (IsIdentity) { continue; } strclass1.Append(columnName + ","); if (CodeCommon.IsAddMark(columnType.Trim())) { strclass2.AppendSpaceLine(3, "strSql.Append(\"'\"+model." + columnName + "+\"',\");"); } else { strclass2.AppendSpaceLine(3, "strSql.Append(\"\"+model." + columnName + "+\",\");"); } } //去掉最后的逗号 strclass1.DelLastComma(); strclass2.Remove(strclass2.Value.Length - 6, 1); strclass1.AppendLine("\");"); strclass.Append(strclass1.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); strclass.AppendSpaceLine(3, "strSql.Append(\" values (\");"); strclass.Append(strclass2.ToString()); strclass.AppendSpaceLine(3, "strSql.Append(\")\");"); //重新定义方法头 if ((dbobj.DbType == "SQL2000" || dbobj.DbType == "SQL2005") && (IsHasIdentity)) { strclass.AppendSpaceLine(3, "strSql.Append(\";select @@IDENTITY\");"); strclass.AppendSpaceLine(3, "object obj = " + DbHelperName + ".GetSingle(strSql.ToString());"); strclass.AppendSpaceLine(3, "if (obj == null)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return 1;"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "else"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "return Convert.ToInt32(obj);"); strclass.AppendSpaceLine(3, "}"); } else { strclass.AppendSpaceLine(3, "" + DbHelperName + ".ExecuteSql(strSql.ToString());"); } strclass.AppendSpace(2, "}"); return(strclass.ToString()); }
/// <summary> /// �õ�GetList()�Ĵ��� /// </summary> /// <param name="_tablename"></param> /// <param name="_key"></param> /// <returns></returns> public string CreatGetList() { //StringPlus strclass = new StringPlus(); //strclass.AppendSpaceLine(2, "/// <summary>"); //strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList"].ToString()); //strclass.AppendSpaceLine(2, "/// </summary>"); //strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); //strclass.AppendSpaceLine(2, "{"); ////strclass.AppendSpaceLine(3, "" + DbParaHead + "Parameter[] parameters = {"); ////strclass.AppendSpaceLine(5, "new " + DbParaHead + "Parameter(\"" + preParameter + _key + "\", " + DbParaDbType + "." + CodeCommon.DbTypeLength(dbobj.DbType, _keyType, "") + ")"); ////strclass.AppendSpaceLine(4, "};"); ////strclass.AppendSpaceLine(3, "parameters[0].Value = strWhere;"); //strclass.AppendSpaceLine(3, "return " + DbHelperName + ".RunProcedure(\"" + ProcPrefix + _tablename + "_GetList" + "\",null,\"ds\");"); //strclass.AppendSpaceLine(2, "}"); //return strclass.Value; StringPlus strclass = new StringPlus(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(string strWhere)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpace(3, "strSql.Append(\"select "); strclass.AppendLine(Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); if ((dbobj.DbType == "SQL2000") || (dbobj.DbType == "SQL2005") || (dbobj.DbType == "SQL2008") || (dbobj.DbType == "SQL2012")) { strclass.AppendLine(); strclass.AppendSpaceLine(2, "/// <summary>"); strclass.AppendSpaceLine(2, "/// " + Languagelist["summaryGetList2"].ToString()); strclass.AppendSpaceLine(2, "/// </summary>"); strclass.AppendSpaceLine(2, "public DataSet GetList(int Top,string strWhere,string filedOrder)"); strclass.AppendSpaceLine(2, "{"); strclass.AppendSpaceLine(3, "StringBuilder strSql=new StringBuilder();"); strclass.AppendSpaceLine(3, "strSql.Append(\"select \");"); strclass.AppendSpaceLine(3, "if(Top>0)"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" top \"+Top.ToString());"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" " + Fieldstrlist + " \");"); strclass.AppendSpaceLine(3, "strSql.Append(\" FROM " + TableName + " \");"); strclass.AppendSpaceLine(3, "if(strWhere.Trim()!=\"\")"); strclass.AppendSpaceLine(3, "{"); strclass.AppendSpaceLine(4, "strSql.Append(\" where \"+strWhere);"); strclass.AppendSpaceLine(3, "}"); strclass.AppendSpaceLine(3, "strSql.Append(\" order by \" + filedOrder);"); strclass.AppendSpaceLine(3, "return " + DbHelperName + ".Query(strSql.ToString());"); strclass.AppendSpaceLine(2, "}"); } return strclass.Value; }