HostFileDataAdapter.Update 被调用以将 DataSet 对象中的更改解析回数据源。
Update 方法和 Fill 方法一样,采用 DataSet 的实例作为参数。
使用数据适配器更新主机文件系统
创建包含
DataSet要更新的信息的对象。或者,您可以通过调用
DataSet.AcceptChanges来覆盖现有DataSet对象的数据。请注意,调用
AcceptChanges对象、DataSet对象或DataTable对象会导致DataRow对象的所有原始值被DataRow对象的当前值覆盖。 如果已修改将行标识为唯一的字段值,则调用AcceptChanges后,原始值不再与数据源中的值匹配。此外,还可以使用
HostFileCommand参数为对象中每个修改行指定 SQL 语句的DataSet输入和输出值。使用要更新的 DataSet 对象进行调用
HostFileDataAdapter.Update。调用
Update该方法时,分析HostFileDataAdapter已进行的更改并执行相应的命令。 如果调用了Update但不存在用于特定更新的相应命令(例如,不存在用于已删除行的DeleteCommand),则会引发异常。如果要使用数据更新数据集,请在
DataSet对象上调用HostFileDataAdapter.Fill。此方法
Update将更改解析回数据源;但是,自上次填充数据集以来,其他客户端可能在数据源中修改了数据。 新行将添加到表中,更新的信息将合并到现有行中。
示例:
下面的代码示例演示如何使用 Fill 和 Update 命令更新数据集。 请注意,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);
}
}