Web Service code not returning array of String

前端 未结 3 1150

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: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
        }
    

提交回复
热议问题