无法将“System.Int32”类型的对象强制转换为“System.String”类型

时间:2012-11-16 13:52:28

标签: c# wcf tsql ado.net guid

我正在尝试通过WCF服务应用程序创建与SQLServer 2008数据库的非常基本的连接以进行测试。尝试远程访问服务时收到以下错误。

  

无法将'System.Int32'类型的对象强制转换为'System.String'

这是我的代码:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class GetCustomers : IGetCustomers
{
    // TODO: Implement the collection resource that will contain the SampleItem instances
    public List<Customer> GetCusts()
    {
        List<Customer> mylist = new List<Customer>();

        using (SqlConnection conn = new SqlConnection("server=(local);database=BillingTest; Integrated Security=true;"))
        {
            conn.Open();

            string cmdStr = String.Format("select firstname, lastname, address from Customers");//, lastname, address, city, state,zipcode,phone, email
            SqlCommand cmd = new SqlCommand(cmdStr, conn);
            SqlDataReader rd = cmd.ExecuteReader();

            if (rd.HasRows)
            {
                Customer cust = new Customer();
                while (rd.Read())
                    cust.setCustomer(rd.GetString(0), rd.GetString(1), rd.GetString(2));
                mylist.Add(cust);
            }
            conn.Close();
        }
        return mylist;
    }
}

public class Customer
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string street { get; set; }

    public void setCustomer(string first, string last, string street)
    {
        // TODO: Complete member initialization
        this.firstname = first;
        this.lastname = last;
        this.street = street;
    }
}

这是我第一次尝试使用WCF服务和sql,我不知道究竟可能出现什么问题。数据库的firstname,lastname和address列都是varchar(50),我没有尝试访问任何Int32列,所以我对错误的来源感到困惑

0 个答案:

没有答案