Reputation: 23
I was trying to create a Web Service that will connect to a SQL database and return all the links stored there.
I've written this code so far, and it only returns the last link that was inserted in the database.
I've created this method that will fetch the links:
public static string GetLinks()
{
string query = string.Format("SELECT Link FROM Linkovi");
try
{
conn.Open();
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
query = reader["Link"].ToString();
}
reader.Close();
}
finally
{
conn.Close();
}
return query;
}
And then I just call this method here like this:
[WebMethod]
public string GetLinks()
{
return ConnectionClass.GetLinks();
}
So, if somebody can help me, I will be really grateful.
Thanks in advance!
Upvotes: 0
Views: 232
Reputation: 133975
Your GetLinks
method only returns a single string. Each time through the loop, you execute:
query = reader["Link"].ToString();
So only one string is ever returned.
If you want to return all of the links, you need to build a list, and add those strings to the list. Or, perhaps, add the strings to a StringBuilder
, separated by newlines or something:
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
sb.Append(reader.["Link"].ToString());
sb.Append("\n");
}
return sb.ToString();
And in your client code, you want to split them out:
string rslt = ConnectionClass.GetLinks();
string[] links = string.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 1
Reputation: 9837
You're only saving the last result. Create a collection, and add the results to the collection, and join the collection values together, or change the return type of the method so that it returns the collection rather than a single string
.
public static string GetLinks()
{
string query = string.Format("SELECT Link FROM Linkovi");
try
{
conn.Open();
command.CommandText = query;
List<string> links = new List<string>();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
links.Add(reader["Link"].ToString());
}
}
}
finally
{
conn.Close();
}
return string.Join(",", links); // You can change the delimiter here to something else.
}
Upvotes: 1
Reputation: 4809
You've declared query as string, so it gets overridden each time. Consider using stringbuilder which is efficient and append the links to it.
string query = string.Format("SELECT Link FROM Linkovi");
StringBuilder result = new StringBuilder();
try
{
conn.Open();
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
result.Append(reader["Link"].ToString());
}
reader.Close();
}
finally
{
conn.Close();
}
return result.ToString();
Upvotes: 1
Reputation: 3538
You are always overwriting the result of the query.
query = reader["Link"].ToString();
Try something like the following:
// store the links in a list
var list = new List<string>();
...
while (reader.Read()) {
list.Add(reader["Link"].ToString());
}
...
// return the list of links
return list;
Upvotes: 1