本文介绍了如何执行具有会话查询的字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从.xml文件获取此sql语句到字符串

I am getting this sql statement from .xml file into string

string sqdata1 =
select * from userdetails where userdetailsid=##DASESSION("userid")##; 



现在我用会话取代## DASESSION


Now i am replacing ##DASESSION with Session

string sqdata1 = sqd;
           if (sqdata1.Contains("##DASESSION("))
           {

               sqdata1 = sqdata1.Replace("##DASESSION(", '"' + "+Session[");
               sqdata1 = sqdata1.Replace(")##", "].ToString()+" + '"');


           }



< / pre>



现在我的string包含查询为

select * from userdetails where userdetailsid ='+ Session [userid]。ToString()+'

如何获取查询与sess离子值...任何帮助......


</pre>

Now My string contains the query as
select * from userdetails where userdetailsid='"+Session["userid"].ToString()+"'
How can i get the query with session values... Any help...

推荐答案

private static void InsertSessionData(SqlCommand command, HttpSessionState session, string commandText)
{
    Regex pattern = new Regex(@"\#\#DASESSION\(""(?<key>[^""]+)""\)\#\#", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

    command.CommandText = pattern.Replace(commandText, match =>
    {
        string key = match.Groups["key"].Value;
        string parameterName = "@" + key;

        command.Parameters.AddWithValue(parameterName, Session[key]);
        return parameterName;
    });
}

...

using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
using (SqlCommand command = new SqlCommand("", connection))
{
    string sqdata1 = "select * from userdetails where userdetailsid=##DASESSION(\"userid\")##";
    InsertSessionData(command, Session, sqdata1);
    ...
}


这篇关于如何执行具有会话查询的字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:15