1

我正在使用此代码在 Gridview 中动态创建行,并使用 LINQ 将这些行插入 DB。现在我想在插入数据库后返回 ID 列表,但我不知道如何。

这是我的代码:

#region [Get List Visitors]
private void GetListVisitors() {
        int rowIndex = 0;
        StringCollection sc = new StringCollection();
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox visitor_txtName = (TextBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("txtName");
                    TextBox visitor_txtAdd = (TextBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("txtAdd");
                    //ASPxComboBox visitor_cbxSex = (ASPxComboBox)grdListVisitors.Rows[rowIndex].Cells[1].FindControl("cbxSex");

                    sc.Add(visitor_txtName.Text + "," + visitor_txtAdd.Text);
                    rowIndex++;
                }
                //Store the current data to StringCollection
                AddListVisitors(sc);
            }
}
#endregion

#region [Add List Visitors]
private void AddListVisitors(StringCollection sc)
{
    try {
        List_Visitor add_visitors = new List_Visitor();
        string[] splitItems = null;
        foreach (string item in sc)
        {
            if (item.Contains(","))
            {
                Travel_DBDataContext db = new Travel_DBDataContext();
                splitItems = item.Split(",".ToCharArray());
                add_visitors.Fullname = splitItems[0];
                add_visitors.add = splitItems[1];
                db.List_Visitors.InsertOnSubmit(add_visitors);
                db.SubmitChanges();
            }

        }      
    }
    catch (Exception addvisitors_error) {
        Response.Write(addvisitors_error.Message + "_method addlistvisitor");
    }
}
#endregion
4

2 回答 2

2

SubmitChanges调用后,add_visitors对象将包含新插入行的主键。

为了获得所有的 id,建议进行一些重构:

// First build an in memory list of objects to add.
var listsToAdd = new List<List_Visitor>();
foreach (string item in sc)
{
  if (item.Contains(","))
  {
    var splitItems = item.Split(",".ToCharArray());

    listsToAdd.Add(new List_Visitor()
    {
      Fullname = splitItems[0],
      add = splitItems[1]
    });
  }
}

// Then add all to the db in one call. Wrap in using block to
// propery dispose of the DataContext and underlying connection.
using(var db = new Travel_DBDataContext())
{
  db.List_Visitors.InsertAllOnSubmit(listsToAdd);
  db.SubmitChanges();
}

// At this point listsToAdd contains all added lists with the generated
// primary key.
于 2013-06-26T18:43:39.887 回答
0

正如 Anders Abel 所说,一旦您调用了 SubmitChanges,ID 将在已插入的对象中更新,但您需要保留对这些对象的引用,现在您使用的是一个不起作用的对象。

#region [Add List Visitors]
private void AddListVisitors(StringCollection sc)
{
    try {
        var newVisitors = new List<List_Visitor>();
        string[] splitItems = null;
        foreach (string item in sc)
        {
            if (item.Contains(","))
            {
                Travel_DBDataContext db = new Travel_DBDataContext();
                splitItems = item.Split(",".ToCharArray());
                var add_visitors = new List_Visitor();
                add_visitors.Fullname = splitItems[0];
                add_visitors.add = splitItems[1];
                db.List_Visitors.InsertOnSubmit(add_visitors);

                newVisitors.Add(add_visitors);
            }
        }    
        db.SubmitChanges();
        // Now newVisitors will contain a collection of List_Visitor
        // objects with the new Id's in.  
    }
    catch (Exception addvisitors_error) {
        Response.Write(addvisitors_error.Message + "_method addlistvisitor");
    }
}
#endregion
于 2013-06-26T19:07:25.997 回答