Oops, sorry, first time I sent wrong files.
using System;
using System.Data;
using System.Data.SqlClient;

public class Test
{
        static SqlConnection conn = new 
SqlConnection("server=(local);Trusted_Connection=yes;database=northwind");
        static SqlDataAdapter da1 = new SqlDataAdapter("select * from foo", conn);
        static SqlDataAdapter da2 = new SqlDataAdapter("select * from bar", conn);
        static SqlDataAdapter da3 = new SqlDataAdapter("select * from baz", conn);
        static DataSet ds = new DataSet();

        public static void Main ()
        {
                conn.Open ();
                Create ();

                SqlCommandBuilder b1 = new SqlCommandBuilder (da1);
                da1.RowUpdating += new SqlRowUpdatingEventHandler (RowUpdatingHandler);
                da1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                da1.Fill (ds, "Foo");
                ds.Tables["Foo"].Rows[0]["FooData"] = "foo2";

                SqlCommandBuilder b2 = new SqlCommandBuilder (da2);
                da2.RowUpdating += new SqlRowUpdatingEventHandler (RowUpdatingHandler);
                da2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                da2.Fill (ds, "Bar");
                ds.Tables["Bar"].Rows[0]["BarData"] = "bar2";

                try {
                        Console.WriteLine ("Calling da1.Update() without table 
parameter.");
                        da1.Update (ds);
                        Console.WriteLine ("Ok.");
                } catch (Exception e) {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }
                try 
                {
                        Console.WriteLine ("Calling da2.Update() without table 
parameter.");
                        da2.Update (ds);
                        Console.WriteLine ("Ok.");
                } 
                catch (Exception e) 
                {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }
                try 
                {
                        Console.WriteLine ("Calling da1.Update() with table name 
parameter.");
                        da1.Update (ds, "Foo");
                        Console.WriteLine ("Ok.");
                } catch (Exception e) {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }
                try 
                {
                        Console.WriteLine ("Calling da2.Update() with table name 
parameter.");
                        da2.Update (ds, "Bar");
                        Console.WriteLine ("Ok.");
                } catch (Exception e) {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }

                Console.WriteLine ("Once again. Fill a table with the default name.");
                SqlCommandBuilder b3 = new SqlCommandBuilder (da3);
                da3.RowUpdating += new SqlRowUpdatingEventHandler (RowUpdatingHandler);
                da3.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                da3.Fill (ds);
                ds.Tables["Table"].Rows[0]["BazData"] = "baz2";

                try {
                        Console.WriteLine ("Calling da1.Update() without table 
parameter.");
                        da1.Update (ds);
                        Console.WriteLine ("Ok.");
                } catch (Exception e) {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }
                try 
                {
                        Console.WriteLine ("Calling da2.Update() without table 
parameter.");
                        da2.Update (ds);
                        Console.WriteLine ("Ok.");
                } catch (Exception e) {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }
                try 
                {
                        Console.WriteLine ("Calling da3.Update() without table 
parameter.");
                        da3.Update (ds);
                        Console.WriteLine ("Ok.");
                } catch (Exception e) {
                        Console.WriteLine ("Caught an exception: ");
                        Console.WriteLine (e);
                }
        }

        private static void Create ()
        {
                SqlCommand cmd = new SqlCommand ();
                cmd.Connection = conn;

                try
                {
                        cmd.CommandText = "drop table foo";
                        cmd.ExecuteNonQuery ();
                }
                catch (Exception)
                {
                }
                try
                {
                        cmd.CommandText = "drop table bar";
                        cmd.ExecuteNonQuery ();
                }
                catch (Exception)
                {
                }
                try
                {
                        cmd.CommandText = "drop table baz";
                        cmd.ExecuteNonQuery ();
                }
                catch (Exception)
                {
                }

                cmd.CommandText = "create table foo (FooId int primary key, FooData 
varchar (100))";
                cmd.ExecuteNonQuery ();
                cmd.CommandText = "insert into foo values (1, 'foo1')";
                cmd.ExecuteNonQuery ();

                cmd.CommandText = "create table bar (BarId int primary key, BarData 
varchar (100))";
                cmd.ExecuteNonQuery ();
                cmd.CommandText = "insert into bar values (1, 'bar1')";
                cmd.ExecuteNonQuery ();

                cmd.CommandText = "create table baz (BazId int primary key, BazData 
varchar (100))";
                cmd.ExecuteNonQuery ();
                cmd.CommandText = "insert into baz values (1, 'baz1')";
                cmd.ExecuteNonQuery ();

                cmd.Dispose ();
        }

        private static void RowUpdatingHandler (object sender, SqlRowUpdatingEventArgs 
e)
        {
                Console.WriteLine ("Updating with command: {0}", 
e.Command.CommandText);
        }
}
Calling da1.Update() without table parameter.
Caught an exception: 
System.InvalidOperationException: Update unable to find TableMapping['Table'] or 
DataTable 'Table'.
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
   at Test.Main()
Calling da2.Update() without table parameter.
Caught an exception: 
System.InvalidOperationException: Update unable to find TableMapping['Table'] or 
DataTable 'Table'.
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
   at Test.Main()
Calling da1.Update() with table name parameter.
Updating with command: UPDATE foo SET FooData = @p1 WHERE ( (FooId = @p2) AND 
((FooData IS NULL AND @p3 IS NULL) OR (FooData = @p4)) )
Ok.
Calling da2.Update() with table name parameter.
Updating with command: UPDATE bar SET BarData = @p1 WHERE ( (BarId = @p2) AND 
((BarData IS NULL AND @p3 IS NULL) OR (BarData = @p4)) )
Ok.
Once again. Fill a table with the default name.
Calling da1.Update() without table parameter.
Caught an exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
   at Test.RowUpdatingHandler(Object sender, SqlRowUpdatingEventArgs e)
   at System.Data.SqlClient.SqlRowUpdatingEventHandler.Invoke(Object sender, 
SqlRowUpdatingEventArgs e)
   at System.Data.SqlClient.SqlDataAdapter.OnRowUpdating(RowUpdatingEventArgs value)
   at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping 
tableMapping)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
   at Test.Main()
Calling da2.Update() without table parameter.
Caught an exception: 
System.NullReferenceException: Object reference not set to an instance of an object.
   at Test.RowUpdatingHandler(Object sender, SqlRowUpdatingEventArgs e)
   at System.Data.SqlClient.SqlRowUpdatingEventHandler.Invoke(Object sender, 
SqlRowUpdatingEventArgs e)
   at System.Data.SqlClient.SqlDataAdapter.OnRowUpdating(RowUpdatingEventArgs value)
   at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping 
tableMapping)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
   at Test.Main()
Calling da3.Update() without table parameter.
Updating with command: UPDATE baz SET BazData = @p1 WHERE ( (BazId = @p2) AND 
((BazData IS NULL AND @p3 IS NULL) OR (BazData = @p4)) )
Ok.

Reply via email to