如何根据多个复选框选择向sql server表添加多行?

时间:2013-03-15 19:06:16

标签: asp.net visual-studio-2010 sql-server-2008 checkbox

我有两个表:ServiceTypeServiceTerm

这些表格彼此相辅相成,并合并到关联表ServiceRate中。一个ServiceType可以有多个ServiceTerms,但并不总是全部。{/ p>

在我的asp.net表单(VB)上,我想将其编码到哪里,我可以使用复选框将每行添加到数据库,以选择适用于所选的ServiceTerms ServiceType

我是asp.net的新手,也是SQL Server 2008的新手。

1 个答案:

答案 0 :(得分:0)

以下是我在其中一个项目中完成的示例: 其中chSportsList是Id的复选框列表

List<ListItem> list_chSportsList = new List<ListItem>();
foreach (ListItem chsli in chSportsList.Items)
{
    Guid gui2 = Guid.Parse("00000000-0000-0000-0000-000000000000");
    try
    {
        gui2 = Guid.Parse(chsli.Value.ToString());
    }
    catch { Response.Redirect("default.aspx"); }
    db = new IRANSportEntities();
    Customer_Sports customer_Sport = new Customer_Sports();
    customer_Sport = db.Customer_Sports.FirstOrDefault(x => x.Id_Customer == gui1 && x.Id_Sports == gui2);
    if (customer_Sport == null)
    {
        if (chsli.Selected == true)
        {
            db = new IRANSportEntities();
            Customer_Sports _save = new Customer_Sports() { Id = Guid.NewGuid(), Id_Customer = gui1, Id_Sports = gui2 };
            db.Customer_Sports.AddObject(_save);
            db.SaveChanges();
        }
    }
    else
    {
        if (chsli.Selected == false)
        {
            db.Customer_Sports.DeleteObject(customer_Sport);
            db.SaveChanges();
        }

    }
}