0

我正在尝试创建一个将返回超过 1 个字符串的 Web 服务。它将返回 4 个字符串。我以前没有 webservices,我过去只返回 true 或 false 值。但现在我需要更多数据。

这是我的网络服务。

[WebMethod]
    public string get_currency(string date, string cur_code) {
        string rtn = "";
        try
        {
            using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"))
            {
                string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
                SqlCommand comm = new SqlCommand(selectSql, conn);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();
                if (dr.Read()) {
                    rtn = dr["date"].ToString() +  dr["code"].ToString() +  dr["banknote_buying"].ToString() + dr["banknote_selling"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            return "Fail";
        }

        return rtn;
    }

我怎样才能将它们作为正确的 SOAP 对象返回

4

2 回答 2

1

我终于做了我想做的事。这是我的代码。

    [WebMethod]
    public Currency_Object get_currency(string date, string cur_code) {

        Currency_Object co = new Currency_Object();
        try
        {
            using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxx;Initial Catalog=MB_DB;Persist Security Info=True;User ID=xxx;Password=xxx"))
            {
                string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
                SqlCommand comm = new SqlCommand(selectSql, conn);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();

                if (dr.Read()) {
                    co.date_ = dr["date"].ToString();
                    co.code_ = dr["code"].ToString();
                    co.banknote_buying_ = dr["banknote_buying"].ToString();
                    co.banknote_selling_ = dr["banknote_selling"].ToString();
                }
            }
        }
        catch (Exception ex) { return null; }
        return co;
    }
}

这就是返回的

 <Currency_Object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <date>06.06.2013 00:00:00</date>
  <code>USD</code>
  <banknote_buying>1,8847</banknote_buying>
  <banknote_selling>1,8922</banknote_selling>
 </Currency_Object>
于 2013-06-14T12:42:50.160 回答
0

尝试返回字符串[]

[WebMethod]
public **string[]** get_currency(string date, string cur_code) {
    **List<string> rtn = new List<string> ();**
    try
    {
        using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"))
        {
            string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
            SqlCommand comm = new SqlCommand(selectSql, conn);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read()) {
                rtn.Add( dr["date"].ToString());
                rtn.Add(dr["code"].ToString());
                 rtn.Add(dr["banknote_buying"].ToString());
                 rtn.Add(dr["banknote_selling"].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        rtn.Add("Fail");
    }

    return rtn.ToArray();
}
于 2013-06-14T12:25:56.617 回答