Hi all !
I have a table in my database, which has 3 attributes. IDFailureContro l,
ControlDate and ControlVersion.
In the following function I test, if the date of today allready exists.
Then I would like to write the new ControlDate or Version into the
database. First i update the dataset, then i create a Insertcommand and
call the update-methode. All datas are in the database, but....
1.) If I only use the InsertCommand, without updating the dataset, the
datas would not be inserted to the database....?
2.) At the end I try to get the new created IDFailureContro l
(autoIncrement) , but it does not exist. Why ? how can I get this ID ?
here my code:
public int WriteControlDat e()
{
DateTime currentDate = DateTime.Today;
DataSet currentDateData s = new DataSet();
int iVersion = 0;
int iIndex = 0;
try
{
this.dbConnecti on.Open();
this.dbAdapter. SelectCommand = new OleDbCommand("S ELECT *
FROM FailureControl ORDER BY IDFailureContro l", this.dbConnecti on);
this.dbAdapter. Fill(currentDat eDatas);
if(currentDateD atas.Tables.Cou nt != 0)
{
int iLastRowIndex =
currentDateData s.Tables[0].Rows.Count - 1;
DataRow lastRow =
currentDateData s.Tables[0].Rows[iLastRowIndex];
DateTime lastControlDate = (DateTime)
lastRow["ControlDat e"];
if(lastControlD ate.Equals(curr entDate) == true)
{
iVersion = (int) lastRow["ControlVersion "];
iVersion++;
}
}
DataRow newRow = currentDateData s.Tables[0].NewRow();
newRow["ControlDat e"] = currentDate;
newRow["ControlVersion "] = iVersion;
currentDateData s.Tables[0].Rows.Add(newRo w);
this.dbAdapter. InsertCommand = new OleDbCommand("I NSERT
INTO FailureControl (ControlDate, ControlVersion) VALUES('" +
currentDate + "', " + iVersion +")", this.dbConnecti on);
this.dbAdapter. Update(currentD ateDatas);
int iNewIndex = currentDateData s.Tables[0].Rows.Count - 1;
newRow = currentDateData s.Tables[0].Rows[iNewIndex];
iIndex = (int) newRow["IDFailureContr ol"];
}
finally
{
this.dbConnecti on.Close();
}
return iIndex;
}
Thanks and regards
I have a table in my database, which has 3 attributes. IDFailureContro l,
ControlDate and ControlVersion.
In the following function I test, if the date of today allready exists.
Then I would like to write the new ControlDate or Version into the
database. First i update the dataset, then i create a Insertcommand and
call the update-methode. All datas are in the database, but....
1.) If I only use the InsertCommand, without updating the dataset, the
datas would not be inserted to the database....?
2.) At the end I try to get the new created IDFailureContro l
(autoIncrement) , but it does not exist. Why ? how can I get this ID ?
here my code:
public int WriteControlDat e()
{
DateTime currentDate = DateTime.Today;
DataSet currentDateData s = new DataSet();
int iVersion = 0;
int iIndex = 0;
try
{
this.dbConnecti on.Open();
this.dbAdapter. SelectCommand = new OleDbCommand("S ELECT *
FROM FailureControl ORDER BY IDFailureContro l", this.dbConnecti on);
this.dbAdapter. Fill(currentDat eDatas);
if(currentDateD atas.Tables.Cou nt != 0)
{
int iLastRowIndex =
currentDateData s.Tables[0].Rows.Count - 1;
DataRow lastRow =
currentDateData s.Tables[0].Rows[iLastRowIndex];
DateTime lastControlDate = (DateTime)
lastRow["ControlDat e"];
if(lastControlD ate.Equals(curr entDate) == true)
{
iVersion = (int) lastRow["ControlVersion "];
iVersion++;
}
}
DataRow newRow = currentDateData s.Tables[0].NewRow();
newRow["ControlDat e"] = currentDate;
newRow["ControlVersion "] = iVersion;
currentDateData s.Tables[0].Rows.Add(newRo w);
this.dbAdapter. InsertCommand = new OleDbCommand("I NSERT
INTO FailureControl (ControlDate, ControlVersion) VALUES('" +
currentDate + "', " + iVersion +")", this.dbConnecti on);
this.dbAdapter. Update(currentD ateDatas);
int iNewIndex = currentDateData s.Tables[0].Rows.Count - 1;
newRow = currentDateData s.Tables[0].Rows[iNewIndex];
iIndex = (int) newRow["IDFailureContr ol"];
}
finally
{
this.dbConnecti on.Close();
}
return iIndex;
}
Thanks and regards
Comment