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