View Single Post
 
Old December 3rd, 2003, 03:13 AM
hueduongit hueduongit is offline
Authorized User
 
Join Date: Nov 2003
Location: Ha Noi, , Vietnam.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

        public static bool UpdateTable(string strTableName, DataSet ds)
        {
            //
            if(ds == null)
                return false;
            //
            if(ds.GetChanges() == null)
                return true;
            //
            OleDbConnection pConn = OleDbConnection (strCon) ;
            pConn.Open();
            if(pConn != null && pConn.State == ConnectionState.Open)//
            {
                OleDbTransaction dbTrans = null;
                string mySelectQuery = "SELECT * FROM " + strTableName;
                try
                {

                    dbTrans = pConn.BeginTransaction(IsolationLevel.ReadCommitte d);
                    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
                    //
                    myDataAdapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
                    myDataAdapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);

                    myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, pConn, dbTrans);
                    OleDbCommandBuilder myCommandBuilder = new OleDbCommandBuilder(myDataAdapter);
                    //
                    int k = myDataAdapter.Update(ds,strTableName);
                    //
                    if((dbTrans != null) && (dbTrans.Connection != null))
                        dbTrans.Commit();
                    if(myCommandBuilder != null)
                        myCommandBuilder.Dispose();
                    if(myDataAdapter != null)
                        myDataAdapter.Dispose();
                    //
                    foreach(DataRow dr in ds.Tables[strTableName].Rows)
                    {
                        if(dr.HasErrors)
                            continue;
                        else
                            dr.AcceptChanges();
                    }
                    DisConnectDB(pConn);//Function disconnect
                    return true;
                }
                catch(Exception myException)
                {
                    if((dbTrans != null) && (dbTrans.Connection != null))
                        dbTrans.Rollback();
                    MsgError = myException.Message.ToString();//return a message error
                    DisConnectDB(pConn);//Function disconnect
                    return false;
                }
            }
            return false;
        }