使用SqlDataAdapter.Update和RowUpdating从gridview更新数据库

问题描述:

我在阅读有关我的问题的其他类似问题后发布此问题,但并不真正了解如何使用这些信息。使用SqlDataAdapter.Update和RowUpdating从gridview更新数据库

我一直主要编写这段代码,看看如何使用SqlDataAdapters从GridView更新数据库。

我写我的GridView在我的aspx页面如下:

<asp:GridView ID="Clients" runat="server"> 
    <Columns> 
     <asp:TemplateField HeaderText="Name" SortExpression="Name"> 
      <ItemTemplate> 
       <asp:Label ID="labelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="textboxName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField EditText="Edit" ShowEditButton="true" /> 
    </Columns> 
</asp:GridView> 

然后在隐藏文件我的代码,我写了下面的代码(数据库仅仅是一个类连接到我的数据库......) :

Database database = new Database(); 
database.open_connection(); 

SqlCommand command = new SqlCommand(query, database.dbConnection); 
SqlDataAdapter adapter = new SqlDataAdapter(command); 

DataTable dataTable = new DataTable(); 
adapter.Fill(dataTable); 

SqlCommandBuilder builder = new SqlCommandBuilder(adapter); 

Clients.AutoGenerateColumns = false; 
Clients.PageIndexChanging += new GridViewPageEventHandler(this.grid_view_page_index_changing); 
Clients.Sorting += new GridViewSortEventHandler(this.grid_view_sorting); 
Clients.RowEditing += new GridViewEditEventHandler(this.row_editing); 
Clients.RowUpdating += new GridViewUpdateEventHandler(this.row_updating); 
Clients.RowCancelingEdit += new GridViewCancelEditEventHandler(this.row_canceling_edit); 
Clients.AllowPaging = true; 
Clients.PageSize = 25; 
Clients.AllowSorting = true; 
Clients.DataSource = dataTable; 
Clients.DataBind(); 

database.close_connection(); 

这一切都工作得很好, GridView排序,编辑,RowCancellingEdit,PageIndexChanging功能等工作,他们应该做的。

我的问题是当我调用RowUpdating函数。

我想要做的就是使用adapter.Update()函数来更新数据库。

它不会对我当前的代码引发任何错误,但它也不会更新数据库。

只要我点击更新,我的GridView中的编辑文本框就消失了,在尝试编辑它之前,我仍保留原始值。

这是我row_updating()功能:

public void row_updating(object sender, GridViewUpdateEventArgs e) { 
    GridViewRow gvr = gridView.Rows[Clients.EditIndex]; 

    TextBox txt = (TextBox)gvr.Cells[0].FindControl("textboxName"); 
    e.NewValues["Name"] = txt.Text; 

    adapter.Update((DataTable)Clients.DataSource); 

    Clients.EditIndex = -1; 
    Clients.DataBind(); 
} 

我想不通为什么它不会更新数据库(可能是因为我完全错了这样做)

我已经看到周围的东西互联网提到了EndEdit()函数,但我不确定这是否适用于此。

如果有人能告诉我我做错了什么,为什么我的数据库不会更新它将不胜感激。

我改变了我的row_updating()功能的代码是多少relevence的不是在这里,而是固定它为我的事情是确保我所用!Page.IsPostBack

所以现在我Page_Load()功能建立在GridView,然后,而不是仅仅结合是不言而喻的数据:

if(!Page.IsPostBack) { 
    Clients.DataBind(); 
} 

虽然不完美的工作,至少现在是更新我的数据库(这是这阙Stion的约,所以我会发布一个新的问题,如果我不能找到一个解决方案,我的新的问题)

有时,它只是造成最令人沮丧的问题,最简单的事情......

您是否为数据适配器创建了UpdateCommand?如果不是,那么我认为您可能需要做到这一点才能成功更新数据。

还要检查这个MSDN链接:

如果INSERT,UPDATE或DELETE没有指定 陈述, Update方法产生 例外。但是,您可以创建一个 SqlCommandBuilder或 OleDbCommandBuilder对象 自动生成单表更新SQL语句 如果设置 一个.NET Framework数据提供程序的SelectCommand属性。然后,您的 未设置的任何 其他SQL语句都由 CommandBuilder生成。该生成逻辑 要求在DataSet中存在关键列信息为 。有关更多 信息,请参阅使用CommandBuilders(ADO.NET)生成命令 。

更新方法在执行更新之前从 中检索第一个映射 中列出的行。 更新然后使用 刷新行,UpdatedRowSource 属性的值。任何返回的行 被忽略。

之后的任何数据被加载回到 数据集,所述引发OnRowUpdated事件 升高,从而允许用户检查 对帐数据集的行和由 命令返回的任何 输出参数。行成功更新 后,将接受对该行 的更改。

当使用更新, 执行的顺序如下:

The values in the DataRow are moved to the parameter values. 

The OnRowUpdating event is raised. 

The command executes. 

If the command is set to FirstReturnedRecord, then the first 

返回的结果被放置在 DataRow中。

If there are output parameters, they are placed in the DataRow. 

The OnRowUpdated event is raised. 

AcceptChanges is called. 
+0

我尝试使用适配器.UpdateCommand = builder.GetUpdateCommand();就像他们在链接中所做的一样,但我仍然得到相同的结果。 – 2010-11-23 10:55:50