Web 服务代码不返回字符串数组

2024-05-04

我想从我的 Web 服务方法返回“abc#xyz#ghi#tru”(其中 # 是分隔符)形式的字符串数组。但是我做不到。这是我当前的网络服务代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebService10
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
 the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        String[] result=new String[40];
        String[] result2 = new String[40];

        [WebMethod]
        public String[] getData()
        {
            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 count(*) from names where name =@name";

                SqlDataReader myReader = myCommand.ExecuteReader();

                //while
                for(int i=0;i<40;i++)
                {
                    if (myReader.Read())
                    {
                         result[i]= myReader["name"].ToString();
                         result2[i] = result[i] + "#";
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }

            return result2;
        }
    }
}

谁能告诉我我的代码有什么问题吗?


尝试这个:

我更改了查询(顺便说一句:查询仍然没有多大意义,但我不知道你真正想要什么)、结果类型和循环。

您也忘记将参数传递给查询。

另外:改变异常处理;在服务器端写入控制台不是一个好主意。

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

我更喜欢不同的方法:

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();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Web 服务代码不返回字符串数组 的相关文章

随机推荐