473,373 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

Returning pk value to DataTable

Hi all,

I've got a question about returning the value of a pk-column to a
DataTable
after inserting a row (via a data-adapter) using MySql5.

Here is the SQL and code concerned:

//================================================== =================
// The table

CREATE TABLE `client` (
`id_client` int(10) unsigned NOT NULL auto_increment,
`name` varchar(40) NOT NULL default '',
PRIMARY KEY (`id_client`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
//================================================== =================
// Stored procedure

CREATE PROCEDURE pInsertClient (OUT id_client INT, IN name VARCHAR(40))
BEGIN

INSERT INTO client(name) VALUES (name);
SET id_client = LAST_INSERT_ID();

END$$

The code:
//================================================== =================
// Creating the data-adapter
....
try
{
// QueryHandler::getConnection() returns a static connection
dataAdapter = gcnew MySqlDataAdapter("SELECT * FROM client",
QueryHandler::getConnection());

// Insert
MySqlCommand^ insertCommand = gcnew MySqlCommand("pInsertClient");
insertCommand->Connection = QueryHandler::getConnection();

insertCommand->CommandType = CommandType::StoredProcedure;

MySqlParameter^ parameter1 = insertCommand->Parameters->Add("?name",
MySqlDbType::VarChar, 40, "name");
parameter1->Direction = ParameterDirection::Input;
MySqlParameter^ parameter2 =
insertCommand->Parameters->Add("?id_client", MySqlDbType::Int32, 32,
"id_client");
parameter2->Direction = ParameterDirection::Output;

dataAdapter->InsertCommand = insertCommand;

dataSet = gcnew DataSet();
dataTable = gcnew DataTable("client");
// dataTable declared elsewhere
dataSet->Tables->Add(dataTable);
dataAdapter->MissingSchemaAction = MissingSchemaAction::AddWithKey;
dataAdapter->Fill(dataTable);

DataColumn^ pkCol = dataTable->Columns["id_client"];
array<DataColumn^>^ columnArray = { pkCol };
dataTable->PrimaryKey = columnArray;
catch (MySqlException^ mE)
{
MessageBox::Show("An exception occured: \n" + mE->Message);
}
....

//================================================== =================

// Performing an insert to the client table

....
DataRow^ newRow = dataTable->NewRow();

newRow["id_client"] = static_cast<int^>(0); // dummy value
newRow["name"] = this->textBox->Text;
dataTable->Rows->Add(newRow);
dataAdapter->Update(dataTable);
....
//================================================== =================

This all works fine. But when I try to optimize the updating of the
DataTable
by changing the last line of code:

dataAdapter->Update(dataTable);

to:

DataSet^ changes = dataSet->GetChanges(DataRowState::Added);
dataAdapter->Update(changes->Tables["client"]);
The primairy key value that is generated in the source database will
not be
returned to the pk-column in the DataTable.

I wonder if it's really necessary to limit the update statement to the
isolated changed rows by calling GetChanges, because when it's applied
to the entire DataTable these rows also have to be determined.

So, can anybody help me out with the following questions:

1. Is it usefull to apply the Update-statement to a narrowed down
selection
of changed rows from the DataTable?

2. If so, how to accomplish this without having to use the RowUpdated
event?
Thanks a lot in advance!

Maart

Nov 13 '06 #1
0 1747

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lekshmi | last post by:
Hi, I have an application written in .Net and my database is SQLserver. In my application i am using SqlAdapters to get the values from the database. But sometimes, values are not returned...
2
by: lekshmi | last post by:
Hi, I have an application written in .Net and my database is SQLserver. In my application i am using SqlAdapters to get the values from the database. But sometimes, values are not returned...
1
by: bubby | last post by:
Should I be concerned about the classic "Deep/Shallow" copy problem when returning objects, specifically a DataTable or DataView from a method? For example, see the code below: private...
3
by: lekshmi | last post by:
Hi, I have an application written in .Net and my database is SQLserver. In my application i am using SqlAdapters to get the values from the database. But sometimes, values are not returned...
6
by: Young J. Putt | last post by:
I've got a list box bound to a Datatable, like this: lstProjects.DataSource = m_oProjectSet.DataTable lstProjects.DisplayMember = "ProjectDesc" lstProjects.ValueMember = "ProjectID" I want to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
5
by: Jim Murphy | last post by:
In creating a C# web service, I am having trouble returning a DataTable object as the result of a web method. I have no problem returning native types like string or int, but cannot return a .NET...
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
5
by: Frank Hauptlorenz | last post by:
Hello, I recognized some days ago, that returning a DataTable blocks my WCF-Service. Is this a known bug? If I add this table to a new DataSet() and return this, it works. Thank you, Frank
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.