代码之家  ›  专栏  ›  技术社区  ›  Shahbaz

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

  •  0
  • Shahbaz  · 技术社区  · 7 年前

    我已经尝试将数据作为字符串发送,并且它工作正常。但现在我想将所有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 回复  |  直到 7 年前
        1
  •  1
  •   Ghadeery    7 年前

    在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);
    

    我已经做到了。只是在网络表单中遇到问题。任何帮助都将不胜感激。我想将dqata作为列表发送到数据库中