tags:

views:

97

answers:

1

Hi! to every One!

I want to delete some record from table ,by running this Query in C# is it Correct or not,

Please help me

SqlCommand cmdRe = new SqlCommand("insert into msisdn_master SELECT *  from tblDeactive
where msisdn in (" + str_MSISDN + ")", cn);
SqlCommand cmdRed = new SqlCommand("delete from tblDeactive where msisdn in ("+str_MSISDN+")", cn);
cmdRe.CommandType = CommandType.Text;
cmdRed.CommandType = CommandType.Text;

note : str_MSISDN is the StringBuilder which stores the Number which is inserted in TextField.

+3  A: 

You should be using proper SQL parameters. NEVER use string building since that leaves you open for injection attacks.

Read this tutorial to learn how to add parameters to SqlCommands.

Soviut
Never say never. If that StringBuilder is not built from user input, it would likely be better than using parameters. Because of the unknown number of ids, the query might not be cached in Sql Server.
Yuriy Faktorovich
@Yuriy: I'll agree that "never" is strong, but dynamic sql is the method of last resort. An "unknown number of IDs" is not an excuse. There are other, better ways around that.
Joel Coehoorn
@Joel: I'll bite, here is your problem. You have an application where the Id field is a Guid. The application has no user entry. It is common for it to delete 10,000 rows in a single table. The rows that are picked cannot be derived from other data in the database. What do you do?
Yuriy Faktorovich
Where did the list of rows come from then? Surely the user didn't generate 10K rows by hand. Odds are it came from the DB somewhere, and your app is missing the piece that records this.
Joel Coehoorn