473,466 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

insert records into two tables another after the other

Hi,

I need to insert records into two tables another after the other. Is the
following code ok or is there a more efficient way of doing this using
sqlcommand object

con.Open()

strSql="insert into table1(column1) values('test1')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

strSql="insert into table2(column1) values('test2')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

con.Close()

Thanks,

Srinivas
Nov 18 '05 #1
3 1563
Hi Reddy,

You might execute them in batch if it is supported.
cmd = New OdbcCommand("insert into table1(column1) values('test1');insert
into table1(column1) values('test2')", con)
.....

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Reddy" <an********@NO.JUNK.MAIL> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
Hi,

I need to insert records into two tables another after the other. Is the
following code ok or is there a more efficient way of doing this using
sqlcommand object

con.Open()

strSql="insert into table1(column1) values('test1')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

strSql="insert into table2(column1) values('test2')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

con.Close()

Thanks,

Srinivas

Nov 18 '05 #2
just write a simle stored proc... pass the values... you dont have to deal
with insert statements and query builder etc... plus if you are using
parameters to pass the stored proc values... you dont have to worry quotes
etcc... it takes care of that...

--

Regards,

HD
"Reddy" <an********@NO.JUNK.MAIL> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
Hi,

I need to insert records into two tables another after the other. Is the
following code ok or is there a more efficient way of doing this using
sqlcommand object

con.Open()

strSql="insert into table1(column1) values('test1')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

strSql="insert into table2(column1) values('test2')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

con.Close()

Thanks,

Srinivas

Nov 18 '05 #3
I don't see a reason to dispose of the first command object, just re-set the
sql, and run it again. But if you want the most efficient way, you should
throw sql parameters in the mix, and forget about the dynamic sql, doing
that you'd probably want to throw out the first command.

"Reddy" <an********@NO.JUNK.MAIL> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
Hi,

I need to insert records into two tables another after the other. Is the
following code ok or is there a more efficient way of doing this using
sqlcommand object

con.Open()

strSql="insert into table1(column1) values('test1')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

strSql="insert into table2(column1) values('test2')"
cmd = New OdbcCommand(strSql, con)
cmd.ExecuteNonQuery()
cmd.Dispose()

con.Close()

Thanks,

Srinivas

Nov 18 '05 #4

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

Similar topics

4
by: Randell D. | last post by:
Folks, I use PHP to write my form data to MySQL. I have a database with about ten tables. I'm trying to fill one table with some dummy data (its a contact manager table holding names of...
1
by: Jay | last post by:
Hi I have a huge table with over 100million records and on regular basis ineed to delete nearly a million records and insert a million records. Currently I delete indexes before going through the...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
3
by: Bob Alston | last post by:
I have a routine to copy data to new versions of my app via insert into sql statements. Unfortunately, due to evolution of my app, sometimes the new version has more restrictive editing than an...
9
by: David Eades | last post by:
Hi all Complete newbie here, so apologies if this is the wrong forum. I've been asked to use mysql and asp to make a simple bidding system (rather like a simple ebay), whereby users can use a...
8
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
0
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
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
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.