如何使用数据适配器更新主机文件系统

HostFileDataAdapter.Update 被调用以将 DataSet 对象中的更改解析回数据源。 Update 方法和 Fill 方法一样,采用 DataSet 的实例作为参数。

使用数据适配器更新主机文件系统

  1. 创建包含 DataSet 要更新的信息的对象。

    或者,您可以通过调用DataSet.AcceptChanges来覆盖现有DataSet对象的数据。

    请注意,调用AcceptChanges对象、DataSet对象或DataTable对象会导致DataRow对象的所有原始值被DataRow对象的当前值覆盖。 如果已修改将行标识为唯一的字段值,则调用 AcceptChanges后,原始值不再与数据源中的值匹配。

    此外,还可以使用 HostFileCommand 参数为对象中每个修改行指定 SQL 语句的 DataSet 输入和输出值。

  2. 使用要更新的 DataSet 对象进行调用 HostFileDataAdapter.Update

    调用 Update 该方法时,分析 HostFileDataAdapter 已进行的更改并执行相应的命令。 如果调用了 Update 但不存在用于特定更新的相应命令(例如,不存在用于已删除行的 DeleteCommand),则会引发异常。

  3. 如果要使用数据更新数据集,请在 DataSet 对象上调用 HostFileDataAdapter.Fill

    此方法 Update 将更改解析回数据源;但是,自上次填充数据集以来,其他客户端可能在数据源中修改了数据。 新行将添加到表中,更新的信息将合并到现有行中。

示例:

下面的代码示例演示如何使用 FillUpdate 命令更新数据集。 请注意,ETCMLogging 和 HostFileUtils 对象分别提供日志记录和实用工具功能。

public void BVTHFDataAdapterInsert(ETCMLogging.Logging logging, string host, string ccsid, string cnstring, HostFileUtils.Utils.HostFileType hostfiletype)  
        {  
            HostFileUtils.Utils u = new HostFileUtils.Utils();  
            logging.LogInfo(host + "::" + hostfiletype.ToString());  
            HostFileUtils.Utils.BvttestsVals[] Datavals = u.InitBvttestsVals();  
  
            try  
            {  
                HostFileConnection cn = new HostFileConnection(cnstring);  
                cn.Open();  
                String SELECT = u.CreateSQLCommand(host, hostfiletype, cnstring, "SELECT", "BVTTESTS");  
                HostFileDataAdapter hfda = new HostFileDataAdapter(SELECT, cn);  
                DataSet ds = new DataSet();                DataSet dsold = new DataSet();                hfda.Fill(ds);                hfda.Fill(dsold);  
                int[] cp = u.CheckColumns(SELECT, cn, logging);  
                u.ValidateDataSet(ds, logging, Datavals, cp);  
                object[] newrow = new object[5];  
                // ('REC129-1','REC129-2',129,1290,'129.645')  
                newrow[cp[0]] = "REC129-1";  
                newrow[cp[1]] = "REC129-2";  
                newrow[cp[2]] = 129;  
                newrow[cp[3]] = 1290;  
                newrow[cp[4]] = 129.645M;  
                ds.Tables[0].Rows.Add(newrow);                  
                int z = hfda.Update(ds);  
                if (z != 1)  
                {  
                    logging.LogFail("a unexpected number of updates::"+z.ToString());  
                }  
                DataSet ds1 = new DataSet();  
                hfda.Fill(ds1);  
                int j = 0;  
                int i = 0;  
                foreach (DataRow row in ds1.Tables[0].Rows)  
                {  
                    string rec = (string)ds1.Tables[0].Rows[j][cp[0]];  
                    if (!rec.Equals("REC129-1"))  
                    {  
                        u.CompareValues((string)ds1.Tables[0].Rows[j][cp[0]], Datavals[i].OUT1_CHAR1, logging);  
                        u.CompareValues((string)ds1.Tables[0].Rows[j][cp[1]], Datavals[i].OUT1_CHAR2, logging);  
                        u.CompareValues((short)ds1.Tables[0].Rows[j][cp[2]], Datavals[i].OUT1_SMALLINT, logging);  
                        u.CompareValues((int)ds1.Tables[0].Rows[j][cp[3]], Datavals[i].OUT1_INTEGER, logging);  
                        u.CompareValues((decimal)ds1.Tables[0].Rows[j][cp[4]], Datavals[i].OUT1_DECIMAL, logging);  
                        j++;  
                        i++;  
                    }  
                    else  
                    {  
                        u.CompareValues((string)ds1.Tables[0].Rows[j][cp[0]], "REC129-1", logging);  
                        u.CompareValues((string)ds1.Tables[0].Rows[j][cp[1]], "REC129-2", logging);  
                        u.CompareValues((short)ds1.Tables[0].Rows[j][cp[2]], 129, logging);  
                        u.CompareValues((int)ds1.Tables[0].Rows[j][cp[3]], 1290, logging);  
                        u.CompareValues((decimal)ds1.Tables[0].Rows[j][cp[4]], 129.645M, logging);  
                        j++;  
                    }  
                }  
                if (j == 0)  
                {  
                    logging.LogFail("No Rows on DataTable!");  
                }  
                z = 0;  
                z = hfda.Update(dsold);  
                if (z != 1)  
                {  
                    logging.LogFail("a unexpected number of updates::" + z.ToString());  
                }  
                DataSet ds2 = new DataSet();  
                hfda.Fill(ds2);  
                u.ValidateDataSet(ds2, logging, Datavals, cp);  
  
                cn.Close();  
            }  
            catch (Exception e)  
            {  
                logging.LogInfo(e.Message);  
                logging.LogFail(e.StackTrace);  
            }  
        }  

另请参阅

使用主机文件适配器和数据集
用于主机文件的 BizTalk 适配器配置