473,394 Members | 1,746 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,394 software developers and data experts.

Insert data to database and get the primarykey

chathura86
227 100+
Expand|Select|Wrap|Line Numbers
  1. public void insertData(String sql)
  2. {
  3.     if(conn != null)
  4.     {
  5.         SqlCommand cmd = new SqlCommand();
  6.         cmd.Connection = conn ; //setting the connection to sql command
  7.         cmd.CommandText = "INSERT INTO Bla bla bla"; //setting the sql
  8.         conn.Open() //open database connection
  9.         cmdUpdateLog.ExecuteNonQuery(); //execute the sql command
  10.  
  11.         cmd.CommandText = "SELECT @@IDENTITY";
  12.         string pri_key = cmd.ExecuteScalar().ToString()
  13.         conn.Close() //close db connection
  14.     }
  15. }
  16.  
i'm using this code to insert data to database and get the primarykey

is their any way to combind the two queries and execute as a single query

because i heard that

INSERT INTO Bla bla bla;SELECT @@IDENTITY";

will work in jdbc

will it work on .net

if so how?

thank you

Chathura Bamunusingh
Oct 21 '08 #1
2 1547
r035198x
13,262 8TB
Insert and select are two different statements. Insert can potentially insert more than one value and so the best information that it can return is the number of rows updated. You could implement it yourself in your own Data Access Class but it would ultimately use two statements.
Oct 21 '08 #2
chathura86
227 100+
Thanks for making it clear

I had doubt about that

So seems that the way i implemented it is the only way

Thanks again for the help

Chathura Bamunusinghe
Oct 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
3
by: PC Datasheet | last post by:
Using code in MyDateBase I create a new database named MyNewDatabase. Now I need to import (or export) using code in MyDatabase tables in another external database named MyData to MyNewDatabase....
0
by: nima | last post by:
Hi I have two tables in my database. PRODUCT and PRODUCT_GROUP. PRODUCT has a foreign key to PRODUCT_GROUP. I generated a typed dataset using VS.net and added a relationship between the two...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
1
by: H5N1 | last post by:
hi there the topic says it all. I have a outer join select statement in tableadapter that populates GridView, I want to make it updatetable, so I need to provide an update command for table...
3
by: Alwin | last post by:
Hey All! I am currently designing a database model for (at first sight) a simple order entry program. The problem I'm currently facing is the exchange of data between the databases of each branch...
4
by: Warex | last post by:
Maybe someone can help, my code works untill it gets to update when it tells me Syntax error in INSERT INTO statement. Thought this was automaticaly created according to MS. Im lost, my code is...
5
nmm32
by: nmm32 | last post by:
I have a DataGrid which displays data with the aid of a procedure. I have tested the procedure inside the database and it is working fine. I have another procedure which adds another row to the...
0
by: cms3023 | last post by:
I have a DataGrid which displays data with the aid of a procedure. I have tested the procedure inside the database and it is working fine. The table inside the database has data that matches with...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.