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

How to insert gridview data into database as a list(Multiple rows) WCF

我已尝试将数据作为字符串发送,但它工作正常。但现在我想将所有 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);
    } }     

在 Iservice1 class 中使用这个

     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();
        }

在界面中使用它。在 class UserDetails

中制作 setter getter
   [OperationContract]

    List<CustomerTable> InsertCustomerDetails(UserDetails userInfo);

我已经做到了。 Web 表单中刚好遇到问题。任何帮助都将得到应用。我想将 dqata 作为列表发送到数据库中