private string _sqltext; //sql代码语句 #endregion Fields #region Methods /*参数说明 * sqlname: 数据库表名、自定义sql代码名、存储过程名等 * * order: 查询操作时sql需要排序的order by参数(,分隔) * returntext: 查询结果为单值 * * qry: 查询分页数据时,是否需要统计总记录数(1 == qry时需要统计) * pagesize: 查询分页数据时,每页记录数 * pageindex: 查询分页数据时,第几页(从1开始) * */ //测试地址 http://localhost/admin/sys/configsql.aspx?db=daSystem&sqlname=slt_pubstree&tag=SqlManager&returntext= protected void Page_Load(object sender, EventArgs e) { if (null != Request["db"]) { _dbhelper = new DBHelper_SQL(Request["db"].ToString()); } else { _dbhelper = new DBHelper_SQL(); } if (null == Request["sqlname"]) //(1)数据库表名。(2)自定义sql代码名称。 { return; } else { _sqlname = Request["sqlname"].ToString().ToLower(); } DataSet ds = new DataSet(); ds = getConfigData(); //Functions.nonull2(ds); if (null != ds) { if (Request["qry"] != null && Request["pagesize"] != null && Request["pageindex"] != null) { Response.ContentType = "text/xml"; //Functions.nonull2(ds); ds.WriteXml(Response.OutputStream); } else { if ( Request["returntext"] != null ) { string rtnval = String.Empty; if (ds.Tables[0].Rows.Count > 0) { rtnval = ds.Tables[0].Rows[0][0].ToString(); } Response.Write(rtnval); } else { Response.ContentType = "text/xml"; ds.WriteXml(Response.OutputStream); } } } else { Response.Write("error:" + _dbhelper._sError); } Response.End(); }
private DataSet getConfigData() { DBHelper_SQL dbhelper2 = new DBHelper_SQL("daSystem"); DataSet ds = new DataSet(); dbhelper2.OpenDB(); ds = dbhelper2.getDataSet("select * from pub_sql where sql_name = '" + _sqlname + "'"); dbhelper2.CloseDB(); if (ds.Tables[0].Rows.Count <= 0) return null; bool ispage = Convert.ToBoolean(ds.Tables[0].Rows[0]["sql_ispage"]); //提取配置信息是否需要包裹分页代码 bool isallcmd = Convert.ToBoolean(ds.Tables[0].Rows[0]["sql_isallcmd"].ToString()); //提取配置信息是否是完整代码 bool isJustPage = false; //是否提交了需要包裹分页代码的必要参数 if (true == ispage && Request["qry"] != null && Request["pagesize"] != null && Request["pageindex"] != null) { if ( null == Request["order"] ) { Response.Write("error:查询分页数据,需要提交order by 参数"); Response.End(); } isJustPage = true; } _sqltext = ds.Tables[0].Rows[0]["sql_text"].ToString().ToLower(); //提取配置信息的sql代码 List<SqlParameter> arrParam = new List<SqlParameter>(); //sql参数 SqlParameter param; string[] paramlist = ds.Tables[0].Rows[0]["sql_params"].ToString().ToLower().Split('|'); //提取配置信息的sql参数 string[] paraminfo; for (int i = 0; i < paramlist.Length; i++) { if (paramlist[i] == "") { continue; } //配置信息异常,直接跳过 paraminfo = paramlist[i].Split(','); if (_sqltext.IndexOf(paraminfo[0]) < 0) { continue; } param = new SqlParameter(); param.SqlDbType = _dbhelper.mapDBType(paraminfo[1]); param.ParameterName = paraminfo[0]; if (Request[paraminfo[0].Substring(1)] != null) { param.Value = Functions.tostr( Request[paraminfo[0].Substring(1)].ToString() ); } //else //{ // param.Value = ""; //} switch (paraminfo[1].ToLower()) { case "varchar": case "nvarchar": case "nchar": case "char": { if (paraminfo.Length > 2 && paraminfo[2] != "") { param.Size = Convert.ToInt32(paraminfo[2]); } break; } case "decimal": { param.Precision = (byte)18; param.Scale = (byte)8; break; } } arrParam.Add(param); } _dbhelper.OpenDB(); DataSet dsNew = new DataSet(); //如果是分页; if (isJustPage) { string order = Request["order"].ToString().Trim(); _sqltext += " order by " + order; dsNew = _dbhelper.getPage( _sqltext.ToString(), order, arrParam, Convert.ToBoolean("1" == Request["qry"].ToString()), Convert.ToInt32(Request["pagesize"].ToString()), Convert.ToInt32(Request["pageindex"].ToString())); } else { dsNew = _dbhelper.getDataSet(_sqltext, arrParam.ToArray()); } _dbhelper.CloseDB(); return dsNew; }
/*参数说明 * db: webconfig数据库连接配置名 * sqlname: 数据库表名、自定义sql代码名、存储过程名等 * opt: 操作类型,如query/insert/update/delete等 * * top: query操作时只取前几条记录 * field: query操作时只需要提取的部分字段(,分隔) * group: query操作时sql需要分组的group by参数(,分隔) * order: query操作时sql需要排序的order by参数(,分隔) * * qry: query分页数据时,是否需要统计总记录数(1 == qry时需要统计) * pagesize: query分页数据时,每页记录数 * pageindex: query分页数据时,第几页(从1开始) * * getds: insert/update 等操作后还需要返回数据集 * pk*: 有pk*做前缀的参数,是针对update/delete等操作时,非主键做条件筛选,该参数字段做为筛选条件 */ protected void Page_Load(object sender, EventArgs e) { //Response.ContentEncoding = System.Text.Encoding.Unicode; //设置返回内容的编码格式 if (null != Request["db"]) { _dbhelper = new DBHelper_SQL(Request["db"].ToString()); } else { _dbhelper = new DBHelper_SQL(); } access(); //Response.Write(Functions.getPKey("pub_tree")); }