参数传递到一个URL执行一个SQL查询后 [英] pass parameters into a url after executing a SQL Query

查看:205
本文介绍了参数传递到一个URL执行一个SQL查询后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近询问如何O写一个查询questtion,幸好你们回答我(谢谢)

<一个href=\"http://stackoverflow.com/questions/10165915/getting-data-from-database-from-different-tables\">My最后一个问题

林在WML网站工作,所以我需要从查询值传递
我找到了一种方法这样做(如下图所示code),但它没有工作。

 &LT;%
//这个查询先前在previous问题提供。        字符串的queryString =选择st.firstname +''+ st.lastname,se.year,c.coursename,c.NumberOfCredits,从学生ST内ri.mark加入RegisteredIn里的ri.StudentId = st.id内部联接本身学期在se.id = ri.SemesterId内部联接上c.id = se.id课程C;

我的方式找到,但没有工作。

 使用(System.Data.SqlClient.SqlConnection连接=新System.Data.SqlClient.SqlConnection(的connectionString))
         {            System.Data.SqlClient.SqlCommand命令=新System.Data.SqlClient.SqlCommand(的queryString,连接);            command.Parameters.AddWithValue(@ StudentId,StudentId);                connection.Open();
                System.Data.SqlClient.SqlDataReader读者= Command.ExecuteReader却();
                如果(reader.HasRows)
                {
                    reader.Read();
                    串_FirstName =读者[0]的ToString();
                    串_lastname =读者[1]的ToString();
                    串_coursename =读者[2]的ToString();
                    串_credits =读者[3]的ToString();
                    串_mark =读者[4]的ToString();                    字符串的URL = string.Format(\"~/StudentInfo.aspx?StudentId={0}&firstname={1}&lastname={2}&coursename={3}&credits={4}&mark={5}\", StudentId,_FirstName,_lastname,_coursename,_credits,_mark);
                    的Response.Redirect(URL);    %GT;

reciving的PARAMATERS页面就不会停

 &LT;%@页面语言=C#AutoEventWireup =真codeFILE =StudentInfo.aspx.cs继承=StudentInfo%GT;&LT;&WML GT;
    &LT;卡&GT;        &所述p为H.;&下;%回复于(Request.Params [StudentId]);%GT;&下; / P&GT;
        &LT; P&GT;&LT; B&GT;姓:&LT; / B&GT; &LT;%的Response.Write(Request.Params [名字]);%GT;&LT; / P&GT;
        &LT; P&GT;&LT; B个名字:&LT; / B&GT; &LT;%的Response.Write(Request.Params [姓氏]);%GT;&LT; / P&GT;
        &LT; P&GT;&LT; B&GT;课程名:其中; / B&GT; &LT;%的Response.Write(Request.Params [课程名]);%GT;&LT; / P&GT;
       &LT; P&GT;&LT; B&GT;学分:其中; / B&GT; &LT;%的Response.Write(Request.Params [学分]);%GT;&LT; / P&GT;
        &LT; P&GT;&LT; B&GT;马克:&LT; / B&GT; &LT;%的Response.Write(Request.Params [标记]);%GT;&LT; / P&GT;    &LT; /卡和GT;
&LT; / WML&GT;

结果就不会停

 第一学期2010
学生:阿林里兹克信用马克的课程名称号
    AAA 3 65
    BBB 3月23日
    CCC 3 65
    DDD 3 58
    EEE 3 70您的这个semster GPA是3.12

我的问题是:
  - 如何提取查询并到URL中的值?
  - 如何我将在W​​ML或在ASP中显示多个值? (因为结果将是多行和值)

感谢您提前


解决方案

您code,因为它是现在从数据库中读取仅第一行。要阅读所有的行,则必须调用阅读器。阅读()在一个循环中,是这样的:

 而(reader.Read())
{
    串_FirstName =读者[0]的ToString();
    串_lastname =读者[1]的ToString()
    ...
}

但我怕传递所有这些信息作为URL参数还真不是好主意。你将不得不加入某种方式从所有行的结果和其他的页面上它们分割回个人价值,这在我看来是多余的。此外,URL长度通常是有限的,所以如果您的查询返回的不仅仅是几排多了,它不会工作。你为什么要在URL传送整个查询的结果?这是比较常见的(而且很容易做的)的URL转移只是学生的标识符(或任何你想显示),并在第二页上,从数据库中重新读取数据。

更新:
如果我理解正确的话,您的实际问题不是如何通过数据库查询结果查询字符串到另一个页面,但你想在页面上显示查询结果。第一步是添加第二页的形式适当的控制(标签,DataGridView的,等等)。那么根据什么控制你使用,你可以绑定控件的数据源,或添加数据的方法手动控制,如在的Page_Load文件StudentInfo.aspx。但因为这是完全不同的主题比你首先问什么,我建议你要问一个问题。另外这个话题在这里dicsussed很多次,所以你可以采取<一看href=\"https://www.google.cz/#sclient=psy-ab&hl=cs&q=display+query+ASP.NET+site%3astackoverflow.com&oq=display+query+ASP.NET+site%3astackoverflow.com&aq=f&aqi=&aql=&gs_l=hp.3...2110l161613l1l161818l24l24l0l0l0l0l155l1887l21j3l24l0.&pbx=1&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=22fdcfe0c4fb8a75&biw=1266&bih=837\"相对=nofollow>这些问题首先的。

I recently asked a questtion on how o write a query and thankfully you guys answered me (Thank you)

My Last Question

Im working on a WML site so I need to pass the values from the query I found a way to do so (shown below in code) , but It didnt work.

    <%   
//this Query was provided earlier in the previous question.

        string queryString = "select st.firstname + ' ' + st.lastname,se.year, c.coursename,c.NumberOfCredits,ri.mark  from Students st inner join RegisteredIn ri on ri.StudentId=st.id inner join Semester se on se.id=ri.SemesterId inner join Courses c on c.id=se.id ";

the Way I found but didnt work

         using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
         {

            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(queryString, connection);

            command.Parameters.AddWithValue("@StudentId",StudentId);

                connection.Open();
                System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    string _firstname = reader[0].ToString();
                    string _lastname = reader[1].ToString();
                    string _coursename = reader[2].ToString();
                    string _credits = reader[3].ToString();
                    string _mark = reader[4].ToString();

                    string url = string.Format("~/StudentInfo.aspx?StudentId={0}&firstname={1}&lastname={2}&coursename={3}&credits={4}&mark={5}", StudentId, _firstname, _lastname, _coursename, _credits, _mark);
                    Response.Redirect(url);

    %>

The page reciving the paramaters will be somthing like

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentInfo.aspx.cs" Inherits="StudentInfo" %>

<wml>
    <card>



        <p><% Response.Write(Request.Params["StudentId"]);%></p>
        <p><b>firstname:</b> <% Response.Write(Request.Params["firstname"]);%></p>
        <p><b>lastname:</b> <% Response.Write(Request.Params["lastname"]);%></p>
        <p><b>coursename:</b> <% Response.Write(Request.Params["coursename"]);%></p>
       <p><b>credits:</b> <% Response.Write(Request.Params["credits"]);%></p>
        <p><b>Mark:</b> <% Response.Write(Request.Params["mark"]);%></p>

    </card>
</wml>

the result will be somthing like

First Semester  2010
Student : Arin Rizk

Course Name     No. of Credit      Mark
    AAA                3            65
    BBB                3            23
    CCC                3            65
    DDD                3            58
    EEE                3            70

Your GPA for this semster is 3.12

My Questions are: - How to extract the values from the query and into the URL ? - How I will display multiple values in WML or in ASP ? (as the results will be multiple lines and of values)

Thank you in advance

解决方案

Your code as it is now will read only first row from database. To read all rows, you must call Reader.Read() in a loop, something like:

while(reader.Read())
{
    string _firstname = reader[0].ToString();
    string _lastname = reader[1].ToString()
    ...
}

But I'm afraid that passing all this information as parameter in URL really is not good idea. You would have to join somehow results from all rows and on the other page split them back to individual values, which seems to me superfluous. Moreover, URL length is usually limited so if your query returns more than just a few rows, it will not work. Why do you want to transfer whole query result in URL? It's more common (and much easier to do) to transfer in URL just identifier of student (or whatever you want to show) and on the second page read data again from database.

UPDATE: If I understand you correctly, your actual question is not how to pass result of database query in querystring to another page, but you want to display result of your query on page. First step is to add appropriate control (Label, DataGridView, etc.) on the form of second page. Then depending on what control you have used you can either bind control to data source, or add data to control manually in method such as Page_Load in StudentInfo.aspx file. But since this is completely different topic than what you were asking first, I suggest you to ask another question. Also this topic was dicsussed here many times, so you may take a look on these questions first.

这篇关于参数传递到一个URL执行一个SQL查询后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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