操作系统2k server
编程软件:web matrix
源代码:
<%@ Page language="C#" debug="true"%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
    void Page_Load(Object sender, EventArgs e) {
    string connectionString;
    string queryString;
    DataSet data = new DataSet();
    OleDbConnection dbConnection;
    OleDbDataAdapter dataAdapter;
    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " +
    "Ole DB Services=-4; " +
    "Data Source=C:\\BeginWithAsp\\Northwind.mdb";
    queryString = "SELECT EmployeeID, FirstName, LastName FROM Employees";
    // set the connection and command
    dbConnection = new OleDbConnection(connectionString);
    dbConnection.Open();
    dataAdapter = new OleDbDataAdapter(queryString, dbConnection);
    // fetch the data
    dataAdapter.Fill(data, "Employees");
    // display the data
    DataGrid1.DataSource = data.Tables["Employees"];
    DataGrid1.DataBind();
    // ---------------------------------------------------------
    // start transaction
    // only to ensure example can be run multiple times
    OleDbTransaction dbTrans = dbConnection.BeginTransaction();
    //-------------------------------------------------------
    // add a new row to the table
    DataTable table;
    DataRow newRow;
    table = data.Tables["Employees"];
    newRow = table.NewRow();
    newRow["FirstName"] = "Norman";
    newRow["LastName"] = "Blake";
    table.Rows.Add(newRow);
    // add another new row. We'll be deleting the one above later
    // and we can't delete existing rows from the database because
    // of referential integrity (every employee also has orders)
    newRow = table.NewRow();
    newRow["FirstName"] = "Beth";
    newRow["LastName"] = "Hart";
    table.Rows.Add(newRow);
    // bind the second grid to the new data
    DataGrid2.DataSource = table;
    DataGrid2.DataBind();
    //-------------------------------------------------------
    // edit an existing row in the table
    DataRow row;
    // the Rows collection is 0 indexed
    // so this will change the fourth row
    row = table.Rows[3];
    row["FirstName"] = "John";
    row["LastName"] = "Hartford";
    // bind the third grid to the new data
    DataGrid3.DataSource = table;
    DataGrid3.DataBind();
    //-------------------------------------------------------
    // delete a row from the table
    table.Rows[table.Rows.Count - 2].Delete();
    // bind the fourth grid to the new data
    DataGrid4.DataSource = table;
    DataGrid4.DataBind();
    //-------------------------------------------------------
    // generate the update commands
    OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
    dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
    dataAdapter.InsertCommand = commandBuilder.GetInsertCommand();
    dataAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();
    //-------------------------------------------------------
    // updqate the data store
    dataAdapter.Update(data, "Employees");
    //-------------------------------------------------------
    // refresh the data in the DataReader and bind it to a new grid
    // to prove that the data store has been updated
    queryString = "SELECT EmployeeID, FirstName, LastName FROM Employees";
    dbConnection.Open();
    OleDbCommand cmd = new OleDbCommand(queryString, dbConnection);
    DataGridUpdated.DataSource =
    cmd.ExecuteReader(CommandBehavior.CloseConnection);
    DataGridUpdated.DataBind();
    // ---------------------------------------------------------
    // Rollback transaction, to reset the data
    dbTrans.Rollback();
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<table width="100%">
<tr>
<td>Original Data</td>
<td>Data with new Row</td>
<td>Data with edited Row</td>
<td>Data with deleted Row</td>
</tr>
<tr>
<td valign="top"><asp:DataGrid id="DataGrid1" runat="server" /></td>
<td valign="top"><asp:DataGrid id="DataGrid2" runat="server" /></td>
<td valign="top"><asp:DataGrid id="DataGrid3" runat="server" /></td>
<td valign="top"><asp:DataGrid id="DataGrid4" runat="server" /></td>
</tr>
</table>
<hr />
Data fetched from the database after the update:<br />
<asp:DataGrid id="DataGridUpdated" runat="server" />
</form>
</body>
</html>
错误提示:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------当分配给命令的连接处于挂起的本地事务中时,命令必须具有事务对象才能执行。该命令的 Transaction 属性尚未初始化。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 当分配给命令的连接处于挂起的本地事务中时,命令必须具有事务对象才能执行。该命令的 Transaction 属性尚未初始化。源错误: 
行 65:     // generate the update commands
行 66:     OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
行 67:     dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
行 68:     dataAdapter.InsertCommand = commandBuilder.GetInsertCommand();
行 69:     dataAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();
 源文件: C:\BeginWithAsp\ch09\UpdataDatabase1.aspx    行: 67 堆栈跟踪: 
[InvalidOperationException: 当分配给命令的连接处于挂起的本地事务中时,命令必须具有事务对象才能执行。该命令的 Transaction 属性尚未初始化。]
   System.Data.OleDb.OleDbConnection.ValidateTransaction(OleDbTransaction transaction) +143
   System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method, Int32& localState) +133
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +61
   System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +69
   System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +5
   System.Data.Common.CommandBuilder.BuildCache(Boolean closeConnection) +552
   System.Data.OleDb.OleDbCommandBuilder.GetUpdateCommand() +23
   ASP.UpdataDatabase1_aspx.Page_Load(Object sender, EventArgs e) in C:\BeginWithAsp\ch09\UpdataDatabase1.aspx:67
   System.Web.UI.Control.OnLoad(EventArgs e) +55
   System.Web.UI.Control.LoadRecursive() +27
   System.Web.UI.Page.ProcessRequestMain() +750