如何将gridview数据作为列表(多行)WCF插入到数据库中

时间:2017-01-19 06:09:02

标签: asp.net wcf wcf-data-services

我尝试将数据作为字符串发送,但它运行正常。但现在我想将所有gridview数据作为列表插入数据库。代码在这里

public interface IService1
{

    [OperationContract]

    string InsertCustomerDetails(UserDetails userInfo);


    [OperationContract]
    [WebGet]

    List<CustomerTable> GetCustomers();
}


public class UserDetails
{

    string Name = string.Empty;    
    string City = string.Empty;
    [DataMember]
    public string name
    {

        get { return Name; }

        set { Name = value; }

    }
    [DataMember]
    public string city
    {

        get { return City; }

        set { City = value; }

    }



public class Service1 : IService1
{




  public string InsertCustomerDetails(UserDetails userInfo)
    {
        using(DataContext db=new DataContext())
        {
            CustomerTable customer = new CustomerTable();
            customer.Name = userInfo.name;
            customer.City = userInfo.city;
              db.CustomerTables.Add(customer);
            db.SaveChanges();
        }
        return "name=  " + userInfo.name + "     city=  " + userInfo.city;
    }
}   

}

WEB表格代码

  protected void ButtonADD_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < GridView2.Rows.Count; i++) {
            UserDetails info = new UserDetails();
            info.name = GridView2.Rows[i].Cells[0].Text;
            info.city = GridView2.Rows[i].Cells[1].Text;
            obj.InsertCustomerDetails(info);
    } }     

1 个答案:

答案 0 :(得分:1)

在Iservice1类中使用此

     public List<CustomerTable> InsertCustomerDetails(UserDetails userInfo)
    {
        using(DataContext db=new DataContext())
        {
            CustomerTable customer = new CustomerTable();
            customer.Name = userInfo.name;
            customer.City = userInfo.city;
              db.CustomerTables.Add(customer);
            db.SaveChanges();
            return db.CustomerTables.ToList();
        }

在界面中使用此功能。在类UserDetails中创建setter getter

   [OperationContract]

    List<CustomerTable> InsertCustomerDetails(UserDetails userInfo);

我做到了这一点。刚刚面对Web窗体中的问题。任何帮助都将得到满足。我想将dqata作为列表发送到数据库