如何从查询中返回多个值 [英] How to return multiple values from query

查看:53
本文介绍了如何从查询中返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用while循环查询但是它无法返回多条记录。我得到的当前输出是一个记录,而我应该总共得到3。



请告知我可能做错了什么 - 非常感谢。



我如何测试查询:

I am currently trying to use while loop for my query but its failing to return multiple records. the current output i am getting is one record, instead i should be getting 3 in total.

Please advice what I may be doing wrong - Many thanks.

How I am testing the query:

protected void Page_Load(object sender, EventArgs e)
        {

            string issue = storyURLSF();
            Label1.Text = Server.HtmlEncode(issue);
        }





我的尝试:





What I have tried:

public static string storyURLSF()
        {
            string article = "";
            int publication = 0;
            int issue = 0;
            int storyid = 0;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["##"].ConnectionString);

            string commandtext = "#####";
            SqlCommand command = new SqlCommand(commandtext, con);
            con.Open();
            command.Parameters.Add(new SqlParameter("title", article));
            command.Parameters.Add(new SqlParameter("PUBLICATION_ID", publication));
            command.Parameters.Add(new SqlParameter("ISSUE_ID", issue));
            command.Parameters.Add(new SqlParameter("STORYID", storyid));

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {

                string name = reader.GetString(0);
                int pub = reader.GetInt32(1);
                int iss = reader.GetInt32(2);
                int sid = reader.GetInt32(3);

                //convertion to string
                string str = Convert.ToString(pub);
                string iss1 = Convert.ToString(iss);
                string sid1 = Convert.ToString(sid);


                var builder = new UriBuilder("http://www.structuredcreditinvestor.com/Article.asp?");
                builder.Port = -1;
                var query = HttpUtility.ParseQueryString(builder.Query);
                // string nm = System.Web.HttpUtility.UrlPathEncode(name);
                query["article"] = name;
                query["ISS"] = str;
                query["PUB"] = iss1;
                query["SID"] = sid1;
                builder.Query = query.ToString();
                string url = builder.ToString();
                return url;
            }
            return article;
        }

推荐答案

你的问题似乎是这个



you problem appears to be with this

var builder = new UriBuilder("http://www.structuredcreditinvestor.com/Article.asp?");
builder.Port = -1;
var query = HttpUtility.ParseQueryString(builder.Query);
// string nm = System.Web.HttpUtility.UrlPathEncode(name);
query["article"] = name;
query["ISS"] = str;
query["PUB"] = iss1;
query["SID"] = sid1;
builder.Query = query.ToString();
string url = builder.ToString();
return url;





循环返回的数据,但是每次都要构建一个新对象而不是扩展/添加到数据 - 因此我非常确定你会看到从数据库中选择的最后一条记录返回的数据 - 使用调试器应该确认这个



我不知道你使用这个构建器/查询的'方法' - 如果我返回一个字符串,我会在while读者之外启动它。阅读()循环,并在循环内扩展/添加它 - 然后当循环结束时,我将返回整个字符串 - 我不知道它是如何工作/转换为你正在使用的工具



you loop through the returned data, but build a new object to return every time rather than extend/add to the data - hence what I'm pretty sure you'll see is the data returned as the last record selected from the database - using the debugger should confirm this

I dont know what 'methodology' you're using with this builder/query - If I were returning a string, I would init it outside of the while reader.Read() loop, and extend/add to it inside the loop - then when the loop is finished, I'd return the entire string - I dont know how that works/translates into the tools you're using


你好miss786,





in while循环你叫返回,所以在第一次只有它会返回当前的url并退出循环,因此它不会再次进入循环。这就是为什么,你没有获得3个值。



另外,如果想要返回3个值而不是返回类型的方法应该是字符串数组或列表或者如果它然后它应该与一些分隔符连接。



以下是你的问题的解决方案,



1 。字符串数组或列表



Hi miss786,


in while loop you are calling "return", so in first time only it will return the current url and go out of the loop, so it will not enter into loop again. that's why , you are not getting 3 values.

Also, if want to return 3 values than return type of the method should be a string array or list or if its string then it should be concatenated with some delimiters.

Below is solution to your problem,

1. string array or list

//public static ArrayList storyURLSF()
public static string[] storyURLSF()
        {
            string article = "";
            string[] articles = new string[3];//if only 3 for sure else use arraylist if it varies
            int count = 0; // to keeep a count to add in array
            ArrayList alArticles = new ArrayList();//if arraylist, then you should change return type
            int publication = 0;
            int issue = 0;
            int storyid = 0;
 
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["##"].ConnectionString);
 
            string commandtext = "#####";
            SqlCommand command = new SqlCommand(commandtext, con);
            con.Open();
            command.Parameters.Add(new SqlParameter("title", article));
            command.Parameters.Add(new SqlParameter("PUBLICATION_ID", publication));
            command.Parameters.Add(new SqlParameter("ISSUE_ID", issue));
            command.Parameters.Add(new SqlParameter("STORYID", storyid));
 
            SqlDataReader reader = command.ExecuteReader();
 
            while (reader.Read())
            {
 
                string name = reader.GetString(0);
                int pub = reader.GetInt32(1);
                int iss = reader.GetInt32(2);
                int sid = reader.GetInt32(3);
 
                //convertion to string
                string str = Convert.ToString(pub);
                string iss1 = Convert.ToString(iss);
                string sid1 = Convert.ToString(sid);
 

                var builder = new UriBuilder("http://www.structuredcreditinvestor.com/Article.asp?");
                builder.Port = -1;
                var query = HttpUtility.ParseQueryString(builder.Query);
                // string nm = System.Web.HttpUtility.UrlPathEncode(name);
                query["article"] = name;
                query["ISS"] = str;
                query["PUB"] = iss1;
                query["SID"] = sid1;
                builder.Query = query.ToString();
                string url = builder.ToString();
                //Your mistake here
                //return url;
                //Solution:
		articles[count] = url;//assign & increase count
                 count ++; //increment
		//or if arraylist(which is better if memory is not an issue)
                alArticles.Add(url);
            }
	    //after looping is finished, then only return
            // make sure return type is array or arraylist
            return articles; //if array
	//OR
	return alArticles;

        }





如果你想要字符串返回类型,然后用分隔符附加每个网址并将其溢出个人价值观。





谢谢,

Prateek



if you want string return type, then append each url by a delimiter and spilt it for individual values.


Thanks,
Prateek


这篇关于如何从查询中返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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