Click here to Skip to main content
15,919,879 members
Home / Discussions / C#
   

C#

 
QuestionConvert Variant in VB to C# Pin
klaydze5-May-09 3:22
klaydze5-May-09 3:22 
AnswerRe: Convert Variant in VB to C# Pin
Le centriste5-May-09 5:19
Le centriste5-May-09 5:19 
GeneralRe: Convert Variant in VB to C# Pin
klaydze6-May-09 2:20
klaydze6-May-09 2:20 
QuestionVariant in VB to C# Pin
klaydze5-May-09 3:13
klaydze5-May-09 3:13 
AnswerRe: Variant in VB to C# Pin
Simon P Stevens5-May-09 4:39
Simon P Stevens5-May-09 4:39 
GeneralRe: Variant in VB to C# Pin
klaydze6-May-09 2:17
klaydze6-May-09 2:17 
GeneralRe: Variant in VB to C# Pin
Simon P Stevens6-May-09 2:57
Simon P Stevens6-May-09 2:57 
GeneralRe: Variant in VB to C# Pin
klaydze6-May-09 3:51
klaydze6-May-09 3:51 
Simon Stevens wrote:
It's unmaintainable and insecure code.

What if some nasty user comes along and calls

GetData("DROP DATABASE [yourdatabase]")

And this is just a minor problem. If you expose the ability to execute arbitrary SQL against your database you can wave goodbye to any serious form of security or data integrity.

If your just writing a prototype or for fun, don't worry about it too much. Just be aware the code is dangerous and should never grow into production code.

If this is production code I would advise a serious rethink about your architecture. You should create a separate data access layer. The DA layer should have strongly typed accessor methods, and they should being using pre written parametrized queries or stored procedures (Google those terms, it's pretty easy to use either with ADO.net)

E.g. you would have a UserDA class with a public User GetUser(String userName) method. The method would run the parametrized query and create a User object to be returned to the caller. This prevents a lot of issues. 1) You are only passing in the user name, not the whole SQL string so the caller can't execute whatever SQL they want. 2) You are returning a strongly typed User object so the caller gets exactly what they are expecting. 3) The use of stored procs or parametrized queries prevents SQL injection attacks. 4) You can build in security if you want and only allow certain users to retrieve and modify certain data.


Hi Simon,

Thanks for the advise.

In short don't passed a sql statement in a parameter method? How about if I want to create a method that is accessible in all my form the same with my previous code.
public object GetData(string SQL)
. How do I secured it?

In your example public User GetUser(string sUser), your just passing a single field where "sUser" is my filter in my sql statement inside the User Method.
SELECT * FROM tblUsers WHERE txtUser = sUser


Thanks and Regards

klaydze

if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}

GeneralRe: Variant in VB to C# Pin
Simon P Stevens6-May-09 4:38
Simon P Stevens6-May-09 4:38 
GeneralRe: Variant in VB to C# Pin
klaydze6-May-09 4:58
klaydze6-May-09 4:58 
GeneralRe: Variant in VB to C# Pin
Simon P Stevens6-May-09 6:14
Simon P Stevens6-May-09 6:14 
GeneralRe: Variant in VB to C# Pin
klaydze7-May-09 3:44
klaydze7-May-09 3:44 
QuestionRich Text Box Flicker Pin
Paul Unsworth5-May-09 3:12
Paul Unsworth5-May-09 3:12 
AnswerRe: Rich Text Box Flicker Pin
Nuri Ismail5-May-09 5:45
Nuri Ismail5-May-09 5:45 
GeneralRe: Rich Text Box Flicker Pin
Paul Unsworth5-May-09 21:15
Paul Unsworth5-May-09 21:15 
QuestionCalling C# function in Java Pin
raesa5-May-09 1:40
raesa5-May-09 1:40 
AnswerRe: Calling C# function in Java Pin
Nagy Vilmos5-May-09 1:54
professionalNagy Vilmos5-May-09 1:54 
GeneralRe: Calling C# function in Java Pin
raesa5-May-09 2:02
raesa5-May-09 2:02 
GeneralRe: Calling C# function in Java Pin
Tom Deketelaere5-May-09 2:25
professionalTom Deketelaere5-May-09 2:25 
GeneralRe: Calling C# function in Java Pin
Nagy Vilmos5-May-09 2:29
professionalNagy Vilmos5-May-09 2:29 
QuestionProcess's changes Pin
lost_in_code5-May-09 1:09
lost_in_code5-May-09 1:09 
Questionc# socket server error Pin
Whydah5-May-09 0:49
Whydah5-May-09 0:49 
AnswerRe: c# socket server error Pin
stancrm5-May-09 2:09
stancrm5-May-09 2:09 
GeneralRe: c# socket server error Pin
Whydah5-May-09 3:20
Whydah5-May-09 3:20 
QuestionConvert string to Font Pin
yesu prakash5-May-09 0:48
yesu prakash5-May-09 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.