Click here to Skip to main content
15,891,703 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i read too many forums but ididnt it
How can i insert one value four table in same time same insert code

What I have tried:

cn.Open()
        'cmd.CommandText = "INSERT INTO T1 (Id) VALUES 111;INSERT INTO T2 (Id) VALUES 111";
        Dim STR1 = "INSERT INTO PERTM001(TCNO)  VALUES(" & TCNO.Text & ") ,INSERT INTO PERTM001(TCNO)  VALUES(" & TCNO.Text & ") "
        Dim Comm As New OleDbCommand(STR1, cn)
        Comm.CommandText = STR1
        Comm.ExecuteNonQuery()
Posted
Updated 8-Mar-20 5:38am

1 solution

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And if you are going to try and do multiple inserts, I'd strongly recommend you look at using a Transaction: OleDbTransaction Class (System.Data.OleDb) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900