HostFileDataAdapter.Update 被呼叫來將 DataSet 物件的變更解析回數據源。 方法 Update 與方法 Fill 一樣,接受 DataSet 的實例作為參數。
使用數據配接器更新主機文件系統
建立
DataSet物件,其中包含您要更新的資訊。或者,您可以使用 對
DataSet.AcceptChanges的呼叫來覆寫現有DataSet對象的數據。請注意,當在
DataSet、DataTable或DataRow物件上呼叫AcceptChanges時,DataRow物件的所有 Original 值都會被DataRow的 Current 值覆寫。 如果您呼叫AcceptChanges之後,將數據列識別為唯一的域值已修改,則原始值不再符合數據源中的值。此外,您可以使用
HostFileCommand參數來指定 物件中每個修改數據列之 SQL 語句的DataSet輸入和輸出值。呼叫
HostFileDataAdapter.Update,並使用您要更新的 DataSet 物件。當您呼叫
Update方法時,會HostFileDataAdapter分析已進行的變更,並執行適當的命令。 如果呼叫Update之後,特定更新沒有適當命令可用 (例如,刪除的資料列沒有DeleteCommand),便會擲回例外狀況 (Exception)。如果您要用資料更新資料集,請在
DataSet物件上呼叫HostFileDataAdapter.Fill。方法
Update會將變更解析回數據源;不過,自上次填入 DataSet 之後,其他用戶端可能已在數據源修改數據。 新的數據列會新增至數據表,且更新的資訊會併入現有的數據列。
範例
下列程式代碼範例示範如何使用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);
}
}