RowUpdated和RowUpdating

12 篇文章 0 订阅

当使用 Update 时,每一个更新的数据行都会发生两个事件。执行顺序如下:

  1. 将 DataRow 中的值移至参数值。

  2. 引发 OnRowUpdating 事件。

  3. 执行命令。

  4. 如果该命令设置为 FirstReturnedRecord,返回的第一项结果将放置在 DataRow 中。

  5. 如果存在输出参数,它们将被放在 DataRow 中。

  6. 引发 OnRowUpdated 事件。

  7. 调用 AcceptChanges。

  8. 下面的示例演示正在使用的 RowUpdating 和 RowUpdated 事件。 

    public   static   void  CreateDataAdapter(
        
    string  connectionString)
    {
        
    using (OleDbConnection connection = new OleDbConnection(connectionString))
        
    {
            OleDbDataAdapter adapter 
    = new OleDbDataAdapter(
                
    "SELECT * FROM Customers WHERE CustomerID = 'ALFKI'", connection);

            adapter.InsertCommand 
    = new OleDbCommand(
                
    "INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)"
                connection);

            adapter.InsertCommand.Parameters.Add(
                
    "@CustomerID", OleDbType.VarChar, 5"CustomerID");
            adapter.InsertCommand.Parameters.Add(
                
    "@CompanyName", OleDbType.VarChar, 30"CompanyName");

            connection.Open();

            DataSet custDS 
    = new DataSet();
            adapter.Fill(custDS, 
    "Customers");

            DataRow custRow 
    = custDS.Tables["Customers"].NewRow();
            custRow[
    "CustomerID"= "NEWCO";
            custRow[
    "CompanyName"= "New Company";
            custDS.Tables[
    "Customers"].Rows.Add(custRow);

            
    // add handlers
            adapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
            adapter.RowUpdated 
    += new OleDbRowUpdatedEventHandler(OnRowUpdated);

            adapter.Update(custDS, 
    "Customers");

            
    // remove handlers
            adapter.RowUpdating -= new OleDbRowUpdatingEventHandler(OnRowUpdating);
            adapter.RowUpdated 
    -= new OleDbRowUpdatedEventHandler(OnRowUpdated);

            
    foreach (DataRow row in custDS.Tables["Customers"].Rows)
            
    {
                
    if (row.HasErrors)
                    Console.WriteLine(row.RowError);
            }

        }

    }


    protected   static   void  OnRowUpdating( object  sender, 
        OleDbRowUpdatingEventArgs args)
    {
        
    if (args.StatementType == StatementType.Insert)
        
    {
            System.IO.TextWriter writer 
    = System.IO.File.AppendText("Inserts.log");
            writer.WriteLine(
    "{0}: Customer {1} Inserted."
                DateTime.Now, args.Row[
    "CustomerID"]);
            writer.Close();
        }

    }


    protected   static   void  OnRowUpdated( object  sender, OleDbRowUpdatedEventArgs args)
    {
        
    if (args.Status == UpdateStatus.ErrorsOccurred)
        
    {
            args.Row.RowError 
    = args.Errors.Message;
            args.Status 
    = UpdateStatus.SkipCurrentRow;
        }

    }

using  System;
using  System.Data;
using  System.Data.OleDb;

class  Class1
{
    
static void Main()
    
{
        
string x = "Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI;Initial Catalog=Northwind";
        CreateDataAdapter(x);
    }


    
public static void CreateDataAdapter(
        
string connectionString)
    
{
        
using (OleDbConnection connection = new OleDbConnection(connectionString))
        
{
            OleDbDataAdapter adapter 
= new OleDbDataAdapter(
                
"SELECT * FROM Customers WHERE CustomerID = 'ALFKI'", connection);

            adapter.InsertCommand 
= new OleDbCommand(
                
"INSERT INTO Customers (CustomerID, CompanyName) VALUES(?, ?)"
                connection);

            adapter.InsertCommand.Parameters.Add(
                
"@CustomerID", OleDbType.VarChar, 5"CustomerID");
            adapter.InsertCommand.Parameters.Add(
                
"@CompanyName", OleDbType.VarChar, 30"CompanyName");

            connection.Open();

            DataSet custDS 
= new DataSet();
            adapter.Fill(custDS, 
"Customers");

            DataRow custRow 
= custDS.Tables["Customers"].NewRow();
            custRow[
"CustomerID"= "NEWCO";
            custRow[
"CompanyName"= "New Company";
            custDS.Tables[
"Customers"].Rows.Add(custRow);

            
// add handlers
            adapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
            adapter.RowUpdated 
+= new OleDbRowUpdatedEventHandler(OnRowUpdated);

            adapter.Update(custDS, 
"Customers");

            
// remove handlers
            adapter.RowUpdating -= new OleDbRowUpdatingEventHandler(OnRowUpdating);
            adapter.RowUpdated 
-= new OleDbRowUpdatedEventHandler(OnRowUpdated);

            
foreach (DataRow row in custDS.Tables["Customers"].Rows)
            
{
                
if (row.HasErrors)
                    Console.WriteLine(row.RowError);
            }

        }

    }


    
protected static void OnRowUpdating(object sender, 
        OleDbRowUpdatingEventArgs args)
    
{
        
if (args.StatementType == StatementType.Insert)
        
{
            System.IO.TextWriter writer 
= System.IO.File.AppendText("Inserts.log");
            writer.WriteLine(
"{0}: Customer {1} Inserted."
                DateTime.Now, args.Row[
"CustomerID"]);
            writer.Close();
        }

    }


    
protected static void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs args)
    
{
        
if (args.Status == UpdateStatus.ErrorsOccurred)
        
{
            args.Row.RowError 
= args.Errors.Message;
            args.Status 
= UpdateStatus.SkipCurrentRow;
        }

    }

今天在使用gridview时出现一个低级错误.

朋友们我有个低级问题,我用GridView编辑功能时,查找不出里面的TextBox
string classname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
其实e.RowIndex怎么点都不出来,要怎么设置才能点出来啊
“System.Web.UI.WebControls.GridViewUpdatedEventArgs”并不包含“RowIndex”的定义

原因是我选用的是RowUpdated而非RowUpdating这两个事件是不同的....

里面RowUpdating才能有这个选项:RowIndex,RowUpdated则没有....

应用得太少....要多加练习了!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值