如何在参数化查询时将Select语句的值作为sql查询返回。 [英] How to return the value of Select statement as sql query when it is parameterized query.

查看:110
本文介绍了如何在参数化查询时将Select语句的值作为sql查询返回。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//public string  qry_getUserRights(int i_DpIntID, int i_GroupID, string Module_Name)
    //{
    //    StringBuilder strBuilder = new StringBuilder();
    //    strBuilder.Append(" SELECT A.UI_ACCESS, A.UI_ADD, A.UI_EDIT, A.UI_VIEW, C.MVIEW, C.MADD, C.MEDIT, C.MACCESS ");
    //    strBuilder.Append(" FROM DPSECURE_UI A, DPSECURE_UI_DPLIST D, USER_RIGHTS_MASTER B, USER_RIGHTS_MASTER_DETAIL C ");
    //    strBuilder.Append(" WHERE A.ID = D.DPSECURE_UI_ID AND ");
    //    strBuilder.Append(" D.ID = C.DPSECURE_UI_DPLIST_ID AND ");
    //    strBuilder.Append(" B.ID = C.USER_RIGHTS_MASTER_ID AND ");
    //    strBuilder.Append(" A.UI_MENU_NAME =:Module_Name AND ");
    //    strBuilder.Append(" B.GROUP_ID =:i_GroupID  AND D.DP_LIST_ID =:i_DpIntID ");

    //    s_dsnstr = o_Cls_Utility.utl_fnGetDSNStr(SessionCheck.s_sessiondpid);
    //    OracleConnect o_Cls_OracleConnect = new OracleConnect(s_dsnstr);
    //    o_Cls_OracleConnect.OracleCommand_PreInit(strBuilder.ToString());

    //    o_Cls_OracleConnect.Parameter_String("Module_Name", Module_Name);
    //    o_Cls_OracleConnect.Parameter_Int32("i_GroupID", i_GroupID);
    //    o_Cls_OracleConnect.Parameter_Int32("i_DpIntID", i_DpIntID);

        
           
    //    return strBuilder.ToString();
    //}

 public void UserRights_Details(string Module_Name, string eximorform, Page t_Page, string SelMode)
        {
            string sql;
            User_Rigths_Qry o_Qry_UserRights = new User_Rigths_Qry();
            sql = o_Qry_UserRights.qry_getUserRights(SessionCheck.s_sessiondpintind, SessionCheck.s_sessionusergroupid, Module_Name);
            DataSet ds;
            StringBuilder str = new StringBuilder();
            s_DSNSTR = utl_fnGetDSNStr(SessionCheck.s_sessiondpid);
            OledbConnect obj_oc = new OledbConnect(s_DSNSTR);
            ds = obj_oc.GetDataSet(s_DSNSTR, sql);

            Control myCtlUTL = default(Control);
            tmp_Ctr = "ctl00$MainContent$";

            if (ds.Tables[0].Rows.Count == 0)
            {
                StringBuilder strBuilder = new StringBuilder();
                strBuilder = new StringBuilder();
                strBuilder.Append("<script language='javascript'>");
                strBuilder.Append("alert('Kindly provide rights to access the module !!!');");
                strBuilder.Append("window.location.href='../Admin/dpsecure.aspx'");
                strBuilder.Append("</script>");
                t_Page.RegisterStartupScript("LastLoginAccessForm", strBuilder.ToString());
                strBuilder = null;
            }
            else
            {
                if (eximorform == "E")
                {
                    if (ds.Tables[0].Rows[0]["UI_VIEW"].ToString() == "T")
                    {
                        if (ds.Tables[0].Rows[0]["MVIEW"].ToString() == "F")
                        {
                            myCtlUTL = t_Page.FindControl(tmp_Ctr + "SearchImageBtn");
                            ((Button)myCtlUTL).Visible = false;

                            myCtlUTL = t_Page.FindControl(tmp_Ctr + "SearchLbl");
                            ((Label)myCtlUTL).Visible = true;
                            ((Label)myCtlUTL).Text = "No View Rights !!!";
                        }
                    }



如果我写上面的查询,它会在


If i write above query it gives exception near

ds = obj_oc.GetDataSet(s_DSNSTR, sql);



所有未绑定的vaiables。


all vaiables not bound.

推荐答案

MainContent
MainContent


;

if (ds.Tables [ 0 ]。Rows.Count == 0
{
StringBuilder strBuilder = new StringBuilder();
strBuilder = new StringBuilder();
strBuilder.Append( < script language ='javascript'>);
strBuilder.Append( alert('请提供访问模块的权限!!!'););
strBuilder.Append( window.location.href ='.. / Admin / dpsecure.aspx' );
strBuilder.Append( < / script>);
t_Page.RegisterStartupScript( LastLoginAccessForm,strBuilder.ToString());
strBuilder = null ;
}
else
{
if (eximorform == E
{
if (ds.Tables [ 0 ]。行[ 0 ] [< span class =code-string>
UI_VIEW]。ToString()== T
{
if (ds.Tables [ 0 ]。行[ 0 ] [ < span class =code-string> MVIEW]。ToString()== F
{
myCtlUTL = t_Page.FindCont rol(tmp_Ctr + SearchImageBtn);
((Button)myCtlUTL).Visible = false ;

myCtlUTL = t_Page.FindControl(tmp_Ctr + SearchLbl);
((标签)myCtlUTL).Visible = true ;
((标签)myCtlUTL).Text = No View Rights !!!;
}
}
"; if (ds.Tables[0].Rows.Count == 0) { StringBuilder strBuilder = new StringBuilder(); strBuilder = new StringBuilder(); strBuilder.Append("<script language='javascript'>"); strBuilder.Append("alert('Kindly provide rights to access the module !!!');"); strBuilder.Append("window.location.href='../Admin/dpsecure.aspx'"); strBuilder.Append("</script>"); t_Page.RegisterStartupScript("LastLoginAccessForm", strBuilder.ToString()); strBuilder = null; } else { if (eximorform == "E") { if (ds.Tables[0].Rows[0]["UI_VIEW"].ToString() == "T") { if (ds.Tables[0].Rows[0]["MVIEW"].ToString() == "F") { myCtlUTL = t_Page.FindControl(tmp_Ctr + "SearchImageBtn"); ((Button)myCtlUTL).Visible = false; myCtlUTL = t_Page.FindControl(tmp_Ctr + "SearchLbl"); ((Label)myCtlUTL).Visible = true; ((Label)myCtlUTL).Text = "No View Rights !!!"; } }



如果我写上面的查询,它会在


If i write above query it gives exception near

ds = obj_oc.GetDataSet(s_DSNSTR, sql);



所有vaiables都没有绑定。


all vaiables not bound.


不确定我是否正确读取代码 - 我猜测评论的代码实际上正在使用中,被调用这一行

Not sure I'm reading the code correctly--I guess that the commented code is actually in use, called by this line
sql = o_Qry_UserRights.qry_getUserRights(SessionCheck.s_sessiondpintind, SessionCheck.s_sessionusergroupid, Module_Name);



什么是OracleConnect类?我猜你没有正确使用它来设置参数。你确定该类中有一些名为Parameter_String和Parameter_Int32的方法,它们取一个参数的名称及其值,并将* add *一个参数添加到类似OracleCommand对象的东西吗?


$ b但是,所有这些都超出了这一点,因为即使假设您的OracleConnect类正在创建命令对象并且参数方法正常工作,您在构建SQL字符串时调用该方法,然后立即处置您的实例OracleConnect及其创建的任何命令对象。您从qry_getUserRights传回的所有内容都是SQL字符串,因此您的所有参数都将丢失。



顺便问一下,为什么使用OracleConnect来获取SQL字符串,然后使用OledbConnect实际执行它(并且是OledbConnect另一个自定义类)?这真的是Oracle吗?



使用Oracle.DataAccess.dll(一个Oracle库),我会做这样的事情,它假定一系列参数并包含一个要包含在参数中的项目数(从VB快速转换的代码,因此可能不完全准确):


What is the OracleConnect class? I'd guess you're not using it correctly to set the parameters. Are you sure there are methods in that class named Parameter_String and Parameter_Int32 that take the name of a parameter and its value and *add* a parameter to something like an OracleCommand object?

All that's beyond the point, however, because even presuming that your OracleConnect class is creating a command object and that the parameter methods work correctly, you're calling that method while you build a SQL string, and then promptly disposing of your instance of OracleConnect and whatever command object it's created. All you pass back from qry_getUserRights is the SQL string, so all your parameters are lost.

By the way, why are you using OracleConnect to get your SQL string, and then OledbConnect to actually execute it (and is OledbConnect another custom class)? Is this really Oracle?

Using Oracle.DataAccess.dll (an Oracle library), I'd do something like this, which assumes an array of parameters and includes a count of items to include in the parameters (code quickly converted from VB, so might not be completely accurate):

OracleConnection conn = New OracleConnection(connectString);
OracleCommand cmd = New OracleCommand(sql, conn);
cmd.Connection.Open();
if (myParams != null && myParams.Length > 0)
{
    cmd.ArrayBindCount = recordCount;
    for (int i = 0; i < myParams.Length; i++)
    {
        cmd.Parameters.Add(myParams[i]);
    }
}
OracleDataReader r = cmd.ExecuteReader();


这篇关于如何在参数化查询时将Select语句的值作为sql查询返回。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆