ASP.NET Tutorial/ Database/OleDbCommandBuilder

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Use OleDbCommandBuilder to create command

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UsingCommandBuilder" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
   <title>Updating a DataSet with DbCommandBuilder</title>
   <form id="form1" runat="server">
      <div id="container">
         <h1>Updating a DataSet with DbCommandBuilder</h1>
         This example illustrates how to write the changes in a 
         <code>DataSet</code> using a <code>DbCommandBuilder</code>
      <asp:GridView ID="grdAuthors" runat="server" />
         <asp:Label ID="labMsg" runat="server" />
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class UsingCommandBuilder : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
       string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
       connString += Server.MapPath("~/Book.mdb") + ";";
       DataSet ds = new DataSet();
         OleDbConnection conn = new OleDbConnection(connString);
         OleDbDataAdapter adapter = new OleDbDataAdapter();
         string sqlSelect = " SELECT * FROM Authors";
         OleDbCommand cmdSelect = new OleDbCommand(sqlSelect, conn);
         adapter.SelectCommand = cmdSelect;
         OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter);
         adapter.Fill(ds, "AuthorsTable");
         DataRow dr1 = ds.Tables["AuthorsTable"].NewRow();
         dr1["AuthorName"] = "A";
         DataRow dr3 = ds.Tables["AuthorsTable"].Rows[0];
         dr3["AuthorName"] = "B";
         adapter.Update(ds, "AuthorsTable");
         grdAuthors.DataSource = ds.Tables["AuthorsTable"].DefaultView;
       catch (Exception ex)
          labMsg.Text = "Error occurred accessing the database";
          labMsg.Text += "<br/>" + ex.Message;