OleDBDataAdapter => Insert and Update of data values in a Database

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
 
D

Dries

Marcel,

A first thing I would like to mention has to do with the use of Commands
(in your case OleDbCommand):
- It is much safer to work with command-parameters instead of building
your SQL-string. So create an OleDbCommand and set the
CommandTextProperty to: "INSERT INTO FailureControl (ControlDate,
ControlVersion) VALUES(?, ?)"
then add the OleDbParameters to the command. Check
http://msdn.microsoft.com/library/d...rlrfsystemdataoledboledbcommandclasstopic.asp
for more information.

Then, to receive the ID of the last inserted row, the best solution
depends on the database-system you are using. Post some more information
please. I then will try to solve you whole problem.

Greetz,
Dries

Marcel said:
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
 
M

Marcel Hug

Hi Dries !
A first thing I would like to mention has to do with the use of Commands
(in your case OleDbCommand):
- It is much safer to work with command-parameters instead of building
your SQL-string. So create an OleDbCommand and set the
CommandTextProperty to: "INSERT INTO FailureControl (ControlDate,
ControlVersion) VALUES(?, ?)"
then add the OleDbParameters to the command. Check
http://msdn.microsoft.com/library/d...rlrfsystemdataoledboledbcommandclasstopic.asp
for more information.

I have seen this solution, but I think it is a little bit confusing. Why
should I not use the popular and standartized SQL-Statements. Why
something new ?
Then, to receive the ID of the last inserted row, the best solution
depends on the database-system you are using. Post some more information
please. I then will try to solve you whole problem.

I'm using a simple MS Access 2000 database. The ID is incremented and a
primary key.

I have a new question:
I have datas in a dataset. I would like to save them in a new table
(just created). Do I have to run trought all recordsets with an Insert
into statement ?

Regards
 
D

Dries

Marcel,
I have seen this solution, but I think it is a little bit confusing. Why
should I not use the popular and standartized SQL-Statements. Why
something new ?
In my humble opinion, it is just easier to work with and it is less
error-sensitive.

The best solution is to catch the OnRowUpdated event of the
OleDbDataAdapter. You can then chech if the action was an Insert action
and if it was you can execute the "SELECT @@IDENTITY"-command to get the
last ID value. More information:
http://msdn.microsoft.com/library/d...cpconRetrievingIdentityOrAutonumberValues.asp

I hope that this will be enough to solve the whole problem. If not, I am
at your disposal.

About your last question, putting data from a dataset into a newly
created table (I suggest you mean a table in a database and not an
ADO.NET DataTable)
When you are using an OleDbDataAdapter you can use the Update method.
This methods accepts DataSet as well as DataTable as a parameter. So you
can just insert all data from whatever DataTable with a single call to
OleDbDataAdapter.Fill. Be carefull with columnnames which can be
different in your database and DataTable. If this is the case, complete
the TableMappings of your Commands (see msdn-docs)

greetz,
Dries
 
M

Marcel Hug

Hi Dries !

Thanks for help
About your last question, putting data from a dataset into a newly
created table (I suggest you mean a table in a database and not an
ADO.NET DataTable)
When you are using an OleDbDataAdapter you can use the Update method.
This methods accepts DataSet as well as DataTable as a parameter. So you
can just insert all data from whatever DataTable with a single call to
OleDbDataAdapter.Fill. Be carefull with columnnames which can be
different in your database and DataTable. If this is the case, complete
the TableMappings of your Commands (see msdn-docs)

In my DataSet are more attributes (columns) then in the new table of the
database (access). I tries the following Mappings:

OleDbCommand dbCommand = new OleDbCommand("INSERT INTO " +
strNewBackupTable + " (Entry, DateMain, Issued, LastUpdt, Modified,
Version, VersionText, VSTATUS) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
this.dbConnection);
this.dbAdapter.InsertCommand = dbCommand;
this.dbAdapter.TableMappings.Add("ENTRY", "Entry");
this.dbAdapter.TableMappings.Add("DATEMAIN", "DateMain");
this.dbAdapter.TableMappings.Add("Issued", "Issued");
this.dbAdapter.TableMappings.Add("LASTUPDT", "LastUpdt");
this.dbAdapter.TableMappings.Add("Modified", "Modified");
this.dbAdapter.TableMappings.Add("VERSION", "Version");
this.dbAdapter.TableMappings.Add("VersionText", "VersionText");
this.dbAdapter.TableMappings.Add("VSTATUS", "VSTATUS");
this.dbAdapter.Update(backupDataSet);

But it does not work. I'm a little bit confused! In the Online MSDN is
the following sentence:

The mapping links the names of columns in the source with those in the
dataset table. For example, information from a column called au_id in
the data source might belong in a column called author_id_number in the
dataset table.

Is the TableMappings for the columns- or the table names ? Because of
the body of the .Add function ???

Thanks !
 
D

Dries

Marcel,

Excuse me, I caused your confusion...
OleDbCommand dbCommand = new OleDbCommand("INSERT INTO " +
strNewBackupTable + " (Entry, DateMain, Issued, LastUpdt, Modified,
Version, VersionText, VSTATUS) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
this.dbConnection);
this.dbAdapter.InsertCommand = dbCommand;
this.dbAdapter.TableMappings.Add("ENTRY", "Entry");
this.dbAdapter.TableMappings.Add("DATEMAIN", "DateMain");
this.dbAdapter.TableMappings.Add("Issued", "Issued");
this.dbAdapter.TableMappings.Add("LASTUPDT", "LastUpdt");
this.dbAdapter.TableMappings.Add("Modified", "Modified");
this.dbAdapter.TableMappings.Add("VERSION", "Version");
this.dbAdapter.TableMappings.Add("VersionText", "VersionText");
this.dbAdapter.TableMappings.Add("VSTATUS", "VSTATUS");
this.dbAdapter.Update(backupDataSet);

But it does not work. I'm a little bit confused! In the Online MSDN is
the following sentence:

The mapping links the names of columns in the source with those in the
dataset table. For example, information from a column called au_id in
the data source might belong in a column called author_id_number in the
dataset table.

Is the TableMappings for the columns- or the table names ? Because of
the body of the .Add function ???

You want to insert data from an ADO.NET DataTable into a table in a
Access database. The best way to do so is, like you already know, using
an OleDbDataAdapter.
But the problem you are having here are the names of the columns and the
tables.
Take e.g. that your access-table is called 'BackupTable' but your
ADO.NET-datatabel is called 'Table1'; in this case you will have to user
the TableMappings property of the dataadapter.
If you want to map column-names, you have to use an OleDbCommand, which
you already have, and add parameters. These parameters have a property
called SourceColumn which refers to the name of the column in the
ADO.NET-datatable.

So, if you want to insert a value in the 'column1'-column of your
accestable, from a column called 'first_column' in your ADO.NET-table
you have to do the following

OleDbCommand command = new OleDbCommand("INSERT INTO [add tablename of
access-db] (column1) VALUES (?)",this.dbconnection);
command.Parameters.Add("column1",[OleDbType of access column],[size of
the acces column],"first_column");

this command can be used as the insertcommand of the dataadapter.

if you want to play safe, you can use the ADO.NET datatable as parameter
for the dataadapter.Fill-method. This way you do not have to think about
mapping the tablename as there is only one table with data to pick data
from.

I hope I have been able to take away the confusion I caused.

greetz,
Dries
 
M

Marcel Hug

Hi Dries !
You want to insert data from an ADO.NET DataTable into a table in a
Access database. The best way to do so is, like you already know, using
an OleDbDataAdapter.
But the problem you are having here are the names of the columns and the
tables.
Take e.g. that your access-table is called 'BackupTable' but your
ADO.NET-datatabel is called 'Table1'; in this case you will have to user
the TableMappings property of the dataadapter.
If you want to map column-names, you have to use an OleDbCommand, which
you already have, and add parameters. These parameters have a property
called SourceColumn which refers to the name of the column in the
ADO.NET-datatable.

Ahh Ok thanks...I'm Sorry, i did not find this information in the net

So, if you want to insert a value in the 'column1'-column of your
accestable, from a column called 'first_column' in your ADO.NET-table
you have to do the following

OleDbCommand command = new OleDbCommand("INSERT INTO [add tablename of
access-db] (column1) VALUES (?)",this.dbconnection);
command.Parameters.Add("column1",[OleDbType of access column],[size of
the acces column],"first_column");

this command can be used as the insertcommand of the dataadapter.

if you want to play safe, you can use the ADO.NET datatable as parameter
for the dataadapter.Fill-method. This way you do not have to think about
mapping the tablename as there is only one table with data to pick data
from.

I hope I have been able to take away the confusion I caused.

Yes you have, but it does not work with my following code.
Could it be a problem, that the DataSet has much more attributes then
the new table ? In my oppinion it should not be a problem because of the
InsertCommand...I tried fill and update !!! nothing is working...
I'm tired of trying to get this data in the database. I will try again
tomorrow.

Thanks for yor help!
Happy New Year !

Here my code:

public void CreateNewBackup(DataSet backupDataSet, BackupTable backupTable)
{
try
{
string strNewBackupTable = this.CreateNewBackupTable(backupTable);

if(this.dbConnection.State == ConnectionState.Closed)
{
this.dbConnection.Open();
}

OleDbCommand dbCommand = new OleDbCommand("INSERT INTO " +
strNewBackupTable + " (Entry, DateMain, Issued, LastUpdt, Modified,
Version, VersionText, VSTATUS) VALUES(?, ?, ?, ?, ?, ?, ?, ?)",
this.dbConnection);
dbCommand.Parameters.Add("Entry", "ENTRY");
dbCommand.Parameters.Add("DateMain", "DATEMAIN");
dbCommand.Parameters.Add("Issued", "Issued");
dbCommand.Parameters.Add("LastUpdt", "LASTUPDT");
dbCommand.Parameters.Add("Modified", "Modified");
dbCommand.Parameters.Add("Version", "VERSION");
dbCommand.Parameters.Add("VersionText", "VersionText");
dbCommand.Parameters.Add("VSTATUS", "VSTATUS");

this.dbAdapter.InsertCommand = dbCommand;
this.dbAdapter.TableMappings.Add(strNewBackupTable,
backupDataSet.Tables[0].TableName);
this.dbAdapter.Fill(backupDataSet);
}
finally
{
this.dbConnection.Close();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top