Web Service code not returning array of String

前端 未结 3 1119

I want to return an array of Strings in the form \"abc#xyz#ghi#tru\" (where # is delimiter) from my web service method . However i m not able to do it . Here is my current web s

相关标签:
3条回答
  • 2021-01-26 18:33

    First, your query returns only number instead of set of rows. So you should use sql like this:

    select name from names
    

    Second, to return array you could use List instead of pre-defined arrays: I would be use the following approach:

    var result = new List<string>();
    while(myReader.Read())
    {
      result.Add(reader["name"].ToString() + "#");
    }
    
    return result.ToArray();
    

    Or if you want to return a string:

    return string.Join("#", result.ToArray())
    
    0 讨论(0)
  • 2021-01-26 18:40

    Try this:

    I changed the query (BTW: the query still doesn't make much sense but I do not know what you really want), the resulting type and the loop.

    You forgot to pass the parameter to the query too.

    Also: change the exception handling; writing to the console on the server side is not a good idea.

    public class Service1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public String getData(string nameFilter)
        {
            String result = "";
    
            SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
            try
            {
                myConnection.Open();
    
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = "select name from names where name =@name";
                myCommand.Parameters.AddWithValue("@name", nameFilter);
                SqlDataReader myReader = myCommand.ExecuteReader();
    
                while(myReader.Read())
                {
                    if(result.Length > 0)
                    {
                        result += "#";
                    }
                    result += myReader["name"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
    
            return result;
        }
    }
    

    EDIT

    I'd prefer a different approach:

    public class Service1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public String[] getData(string nameFilter)
        {
            var names = new List<string>();
            SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
            try
            {
                myConnection.Open();
    
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = "select name from names where name = @name";
                myCommand.Parameters.AddWithValue("@name", nameFilter);
                SqlDataReader myReader = myCommand.ExecuteReader();
    
                while(myReader.Read())
                {
                    names.Add(myReader["name"].ToString());
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
    
            return names.ToArray();
        }
    }
    
    0 讨论(0)
  • 2021-01-26 18:46
     [WebMethod]
        public string getData()//changed to return string
        {
    
            SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
            try
            {
                myConnection.Open();
    
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = "select name from names";//you can make it select distinct
    
                SqlDataReader myReader = myCommand.ExecuteReader();
                string toReturn = "";
                while(myReader.Read())
                {
                    if (myReader.Read())
                    {
                         toReturn += myReader["name"].ToString() + "#";
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
    
            return toReturn; //# as delimiter
        }
    
    0 讨论(0)
提交回复
热议问题