问题描述
我想从我的web服务方法返回一个字符串数组的形式ABC#XYZ#GHI#TRU(其中#是分隔符)。但是我为没能做到这一点。这是我目前的网络服务code:
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 service code :
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;
}
}
}
谁能告诉我什么地方错了我的code?
Can anyone tell me what's wrong with my code ?
推荐答案
试试这个:
我改变了查询(BTW:查询仍然没有多大意义,但我不知道你真正想要的),所产生的类型和循环
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;
}
}
修改
我preFER一种不同的方法:
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();
}
}
这篇关于Web服务code没有返回字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!