我有一个挑战,我最近写了一个Web服务,它能够从MSSQL服务器获取数据并以xml显示。
看起来像这样
客户.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Customer
/// </summary>
public class Customer
{
public string fullname { get; set; }
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string id_type { get; set; }
public string id_number { get; set; }
public string dob { get; set; }
public string bvn { get; set; }
}
从 Web 服务使用它来检索数据如下:
FetchInformationBVNService.cs
public Customer GetCustomerNameWithBVN(string bvn_search)
{
using (SqlConnection cn = new SqlConnection(constring))
{
cn.Open();
string q = "select fullname,address,city,state,id_type,id_number,date_ofbirth,bvn from account_info where bvn =@bvn";
using (SqlCommand cmd = new SqlCommand(q, cn))
{
cmd.Parameters.AddWithValue("@bvn", bvn_search);
using (SqlDataReader rd = cmd.ExecuteReader())
{
if (rd.Read())
{
return new Customer
{
fullname = rd["fullname"].ToString(),
address = rd["address"].ToString(),
city = rd["city"].ToString(),
state = rd["state"].ToString(),
id_type = rd["id_type"].ToString(),
id_number = rd["id_number"].ToString(),
dob = rd["date_ofbirth"].ToString(),
bvn = rd["bvn"].ToString()
};
}return null;
}
}
}
}
一切正常,在IIS Express上测试,不用担心。 现在我创建了一个 Winform,我想使用相同的 Web 服务,以便它可以使用以下命名控件填充一些 textField: 全名。文本,地址。文本,城市。文本,状态。文本,id_type。文本,id_number。文本,bvn。发短信。
它只是根本不填充。我已经从解决方案资源管理器 -> 添加 -> 服务参考 -> 高级 -> 添加Web 参考中添加了 Web 参考,然后将那里的引用重命名为newAccountSearchByBVN
这使我们有了这样的东西:
引用
**newAccountSearchByBVN.FetchInformationBVNService**
其中newAccountSearchByBVN
是命名空间,FetchInformationBVNService
是服务。
现在我反过来做了这样的事情来检索以下信息:
newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
Still wont work..
问题是,我如何检索信息并填充表单字段以显示数据库中的信息。
尝试从数据库填充数据
编辑:现在我尝试像这样填充来自Web服务的数据:
private void button5_Click(object sender, EventArgs e)
{
newAccountSearchByBVN.Customer cs = new newAccountSearchByBVN.Customer();
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
string a = nacc.GetCustomerNameWithBVN(bvn_search.Text).ToString();
bool check = bool.Parse(a);
if (check)
{
cs.fullname = fullname.Text;
cs.address = address.Text;
cs.city = city.Text;
cs.state = state.Text;
cs.id_type = id_type.Text;
cs.id_number = id_number.Text;
}
}
一些变化:
private void button5_Click(object sender, EventArgs e)
{
newAccountSearchByBVN.Customer cs; //no need to create new object, you'll be receiving it from server
newAccountSearchByBVN.FetchInformationBVNService nacc = new newAccountSearchByBVN.FetchInformationBVNService();
cs = nacc.GetCustomerNameWithBVN(bvn_search.Text); //instead of saving your Customer to string, save it to cs variable you already
if (cs != null) //check if you've received object, it's not null
{
//order was wrong. You want to populate text boxes, and you were taking data from text boxes here...
fullname.Text = cs.fullname;
address.Text = cs.address;
city.Text = cs.city;
state.Text = cs.state;
id_type.Text = cs.id_type;
id_number.Text = cs.id_number;
}
}
其他提示,您应该考虑对正确的数据使用正确的类型。例如(id_type
和id_number
不应该string
,而是int
(