簡體   English   中英

使用SqlDataAdapter.Update和RowUpdating從gridview更新數據庫

[英]Using SqlDataAdapter.Update and RowUpdating to update a database from a gridview

在閱讀有關我的問題的其他類似問題之后,我發布了這個問題,但並沒有真正理解我如何使用這些信息。

我主要在編寫此代碼,以了解如何使用SqlDataAdapters從GridView更新數據庫。

我在我的aspx頁面中編寫GridView,如下所示:

<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的Sorting,Editing,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()函數的內容,但是我不確定在這里是否適用。

如果有人能告訴我我在做什么錯以及為什么我的數據庫不會更新,將不勝感激。

您是否為數據適配器創建了UpdateCommand。 如果沒有,那么我認為您可能需要這樣做才能成功更新數據。

還要檢查此MSDN鏈接:

如果未指定INSERT,UPDATE或DELETE語句,則Update方法將生成異常。 但是,如果設置.NET Framework數據提供程序的SelectCommand屬性,則可以創建SqlCommandBuilder或OleDbCommandBuilder對象以自動為單表更新生成SQL語句。 然后,CommandBuilder會生成您未設置的任何其他SQL語句。 此生成邏輯要求鍵列信息出現在數據集中。 有關更多信息,請參見使用CommandBuilders(ADO.NET)生成命令。

在執行更新之前,Update方法從第一個映射中列出的表中檢索行。 然后,更新將使用UpdatedRowSource屬性的值刷新行。 返回的所有其他行都將被忽略。

將任何數據加載回DataSet之后,將引發OnRowUpdated事件,使用戶可以檢查協調后的DataSet行以及該命令返回的所有輸出參數。 成功更新一行后,將接受對該行的更改。

使用更新時,執行順序如下:

 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. 

我更改了row_updating()函數代碼,此處的相關性不大,但是為我修復的問題是確保使用了!Page.IsPostBack

所以現在我的Page_Load()函數設置了GridView,然后不僅僅是綁定數據:

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

盡管運行不正常,但至少現在正在更新我的數據庫(此問題的目的是什么,因此,如果找不到新問題的解決方案,我將發布一個新問題)

有時候,最簡單的事情會導致最令人沮喪的問題...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM