M
Marcel Hug
Hi all !
I have a table in my database, which has 3 attributes. IDFailureControl,
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 IDFailureControl
(autoIncrement), but it does not exist. Why ? how can I get this ID ?
here my code:
public int WriteControlDate()
{
DateTime currentDate = DateTime.Today;
DataSet currentDateDatas = new DataSet();
int iVersion = 0;
int iIndex = 0;
try
{
this.dbConnection.Open();
this.dbAdapter.SelectCommand = new OleDbCommand("SELECT *
FROM FailureControl ORDER BY IDFailureControl", this.dbConnection);
this.dbAdapter.Fill(currentDateDatas);
if(currentDateDatas.Tables.Count != 0)
{
int iLastRowIndex =
currentDateDatas.Tables[0].Rows.Count - 1;
DataRow lastRow =
currentDateDatas.Tables[0].Rows[iLastRowIndex];
DateTime lastControlDate = (DateTime)
lastRow["ControlDate"];
if(lastControlDate.Equals(currentDate) == true)
{
iVersion = (int) lastRow["ControlVersion"];
iVersion++;
}
}
DataRow newRow = currentDateDatas.Tables[0].NewRow();
newRow["ControlDate"] = currentDate;
newRow["ControlVersion"] = iVersion;
currentDateDatas.Tables[0].Rows.Add(newRow);
this.dbAdapter.InsertCommand = new OleDbCommand("INSERT
INTO FailureControl (ControlDate, ControlVersion) VALUES('" +
currentDate + "', " + iVersion +")", this.dbConnection);
this.dbAdapter.Update(currentDateDatas);
int iNewIndex = currentDateDatas.Tables[0].Rows.Count - 1;
newRow = currentDateDatas.Tables[0].Rows[iNewIndex];
iIndex = (int) newRow["IDFailureControl"];
}
finally
{
this.dbConnection.Close();
}
return iIndex;
}
Thanks and regards
I have a table in my database, which has 3 attributes. IDFailureControl,
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 IDFailureControl
(autoIncrement), but it does not exist. Why ? how can I get this ID ?
here my code:
public int WriteControlDate()
{
DateTime currentDate = DateTime.Today;
DataSet currentDateDatas = new DataSet();
int iVersion = 0;
int iIndex = 0;
try
{
this.dbConnection.Open();
this.dbAdapter.SelectCommand = new OleDbCommand("SELECT *
FROM FailureControl ORDER BY IDFailureControl", this.dbConnection);
this.dbAdapter.Fill(currentDateDatas);
if(currentDateDatas.Tables.Count != 0)
{
int iLastRowIndex =
currentDateDatas.Tables[0].Rows.Count - 1;
DataRow lastRow =
currentDateDatas.Tables[0].Rows[iLastRowIndex];
DateTime lastControlDate = (DateTime)
lastRow["ControlDate"];
if(lastControlDate.Equals(currentDate) == true)
{
iVersion = (int) lastRow["ControlVersion"];
iVersion++;
}
}
DataRow newRow = currentDateDatas.Tables[0].NewRow();
newRow["ControlDate"] = currentDate;
newRow["ControlVersion"] = iVersion;
currentDateDatas.Tables[0].Rows.Add(newRow);
this.dbAdapter.InsertCommand = new OleDbCommand("INSERT
INTO FailureControl (ControlDate, ControlVersion) VALUES('" +
currentDate + "', " + iVersion +")", this.dbConnection);
this.dbAdapter.Update(currentDateDatas);
int iNewIndex = currentDateDatas.Tables[0].Rows.Count - 1;
newRow = currentDateDatas.Tables[0].Rows[iNewIndex];
iIndex = (int) newRow["IDFailureControl"];
}
finally
{
this.dbConnection.Close();
}
return iIndex;
}
Thanks and regards