Database, DataSet, AutoNumber,.... Help ??

H

Hai Nguyen

Hi everyone

I'm devloping a wep application which needs to access database frequently
(for now access database). I got stuck.

The first page, I ask for several questions from customers and holds their
answers. Let make it simple, the questions are

Name:
Address
Phone
Email:


In database has:
ID Name Address Phone Email

ID is a counter which will increment automatically after I insert the the
row

There is another table which reuses the ID from the table above
ID ABC DEF GHI

I would like to know what is the idea behind this solutions. I want to know
right after I insert the record including Name,Address,Phone,Email. I want
to have the ID right after that, store it somewhere for later use.

Thanks for any help
 
F

Felix Wang

You can use "SELECT SCOPE_IDENTITY()" or "SELECT @@IDENTITY" to select the
value back. Use the SqlCommand.ExecuteScalar() method.

I hope this helps.
 
M

Michelle Stone

I am not sure about this reply. I am just attempting a
solution as I noticed that no one else has responded.

I haven't really understood your question. Do you just
want to know the ID of the record you just insert?
use "SELECT @@IDENTITY" to return the ID of the record
you just insert

But note that this has to be done using the same
CONNECTION u used for inserting the statement.

The following is fine:

sqlConnection.Open ();
sqlCommand.CommandText = "INSERT INTO Table (a, b) VALUES
(10,20);SELECT @@IDENTITY);

object id;
id = sqlCommand.ExecuteScalar();

int myID = Convert.ToInt32 (id);

sqlConnection.Close ();

You also mentioned something about accessing the
database "frequently". If this was your problem, then try
caching the dataset.

Regards,

Michelle

-----Original Message-----
Hi everyone

I'm devloping a wep application which needs to access database frequently
(for now access database). I got stuck.

The first page, I ask for several questions from customers and holds their
answers. Let make it simple, the questions are

Name:
Address
Phone
Email:


In database has:
ID Name Address Phone Email

ID is a counter which will increment automatically after I insert the the
row

There is another table which reuses the ID from the table above
ID ABC DEF GHI

I would like to know what is the idea behind this solutions. I want to know
right after I insert the record including
Name,Address,Phone,Email. I want
 
H

Hai Nguyen

Thank you so much. It works great


Michelle Stone said:
I am not sure about this reply. I am just attempting a
solution as I noticed that no one else has responded.

I haven't really understood your question. Do you just
want to know the ID of the record you just insert?
use "SELECT @@IDENTITY" to return the ID of the record
you just insert

But note that this has to be done using the same
CONNECTION u used for inserting the statement.

The following is fine:

sqlConnection.Open ();
sqlCommand.CommandText = "INSERT INTO Table (a, b) VALUES
(10,20);SELECT @@IDENTITY);

object id;
id = sqlCommand.ExecuteScalar();

int myID = Convert.ToInt32 (id);

sqlConnection.Close ();

You also mentioned something about accessing the
database "frequently". If this was your problem, then try
caching the dataset.

Regards,

Michelle


Name,Address,Phone,Email. I want
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top