OleDBDataAdapter => Insert and Update of data values in a Database
- From: Marcel Hug <marcel.c.hug@xxxxxxxxxx>
- Date: Sat, 31 Dec 2005 11:07:15 +0100
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 .
- Follow-Ups:
- Prev by Date: Re: Why is C# 450% slower than C++ on nested loops ??
- Next by Date: C# Unsafe Bug
- Previous by thread: Folder View
- Next by thread: Re: OleDBDataAdapter => Insert and Update of data values in a Database
- Index(es):
Loading