private string CreateExists() { StringBuilder source = new StringBuilder(); //创建方法头 source.AppendLine(" public bool _Exists" + string.Format("({0} {1})", AttributeHelper.PrimaryKeyTypeToCs(primaryKey), primaryKey.Name) + "{"); source.AppendLine(tryStart()); //创建SQL语句头 source.AppendLine(string.Format("strSql.Append(\"select count(1) from {0}\");", tableInfo.Table.Name)); //创建参数列表 source.AppendLine(string.Format("strSql.Append(\" where {0}=@{0} \");", primaryKey.Name)); //创建参数赋值 source.AppendLine("SqlParameter[] parameters = {new SqlParameter(\"@" + primaryKey.Name + "\", SqlDbType." + primaryKey.Type.ToString() + ")};parameters[0].Value = " + primaryKey.Name + ";"); //创建执行命令 source.AppendLine("return DbHelperSQL.Exists(strSql.ToString(), parameters);"); source.AppendLine(tryEnd()); source.AppendLine("}"); return(source.ToString()); }
private string CreateGetModel() { StringBuilder sources = new StringBuilder(); sources.AppendLine("public " + tableInfo.TypeFullName + " _GetModel(" + AttributeHelper.PrimaryKeyTypeToCs(primaryKey) + " " + primaryKey.Name + "){"); sources.AppendLine(tryStart()); sources.AppendLine("DataSet dst = new DataSet();"); //创建参数赋值 sources.AppendLine("SqlParameter[] parameters = {new SqlParameter(\"@" + primaryKey.Name + "\", SqlDbType." + primaryKey.Type.ToString() + ")};parameters[0].Value = " + primaryKey.Name + ";"); sources.AppendLine(string.Format("dst=_GetList(1,\" {0}=@{0} \",parameters,\"\");", primaryKey.Name)); sources.AppendLine("if (dst.Tables[0].Rows.Count < 1){"); sources.AppendLine("return null;"); sources.AppendLine("}"); sources.AppendLine("return dst.Tables[0].ToList<" + tableInfo.TypeFullName + ">()[0];"); sources.AppendLine(tryEnd()); sources.AppendLine("}"); return(sources.ToString()); }
private string CreateDelete() { StringBuilder source = new StringBuilder(); //创建方法头 source.AppendLine(" public void _Delete(" + AttributeHelper.PrimaryKeyTypeToCs(primaryKey) + " " + primaryKey.Name + ", SqlTranExtensions _SqlTranExtensions){"); source.AppendLine(tryStart()); //创建SQL语句头 source.AppendLine(string.Format("strSql.Append(\"delete from {0} \");", tableInfo.Table.Name)); //创建以主键为条件语句 source.AppendLine(AttributeHelper.GetPrimaryKeyWhere(primaryKey)); //创建参数列表 source.AppendLine("SqlParameter[] parameters = {" + AttributeHelper.GetSqlParameterDeclare(new ColumnAttribute[] { primaryKey }) + "};"); //创建参数赋值 source.AppendLine(AttributeHelper.GetSqlParameterValue(new ColumnAttribute[] { primaryKey }).Replace("model.", "")); //创建执行命令 source.AppendLine("if(_SqlTranExtensions != null){_SqlTranExtensions.Add(strSql.ToString(),parameters);return;}"); //判断数据库 source.AppendLine(@"if(DataHelper.IsAccess){ DbHelperOleDb.ExecuteSql(strSql.ToString(), DataHelper.GetOleDbParameterBySqlParameter(parameters)); }"); source.AppendLine(@"else{ DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); }"); source.AppendLine(tryEnd()); source.AppendLine("}"); return(source.ToString()); }