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

C#

 
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 
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 
Use a SqlCommand and insert parameters into the command.

like this:
public static String GetUser(String userId)
{
    // Create your connection however you normally do it.
    SqlConnection connection = new SqlConnection();

    // Create a sql query with parameters in any place you need user data to be inserted.
    String sqlQuery = "SELECT userName FROM Users WHERE UserId = @UserIdParameter";
    SqlCommand command = new SqlCommand(sqlQuery, connection);

    // Add a parameter for each value entered by the user.
    // By doing it this way, the caller can never inject extra SQL that.
    command.Parameters.Add(new SqlParameter("@UserIdParameter", userId));

    // Execute the reader to run the query.
    using (SqlDataReader reader = command.ExecuteReader())
    {
        if (reader.HasRows)
        {
            return reader.GetString(0);
        }
        else
        {
            return String.Empty;
        }
    }
}
This means that your caller can't inject sql in because they only have control over the parameter and because it's a parameter, when it the query is executed, the parameter will be validated and surrounded with quotes and any command characters will be removed to prevent injection attacks.

Read more here:
SQL Injection Attacks and Some Tips on How to Prevent Them[^]

Simon

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 
AnswerRe: Convert string to Font Pin
Michael Bookatz5-May-09 1:02
Michael Bookatz5-May-09 1:02 
AnswerRe: Convert string to Font Pin
Baran M5-May-09 3:44
Baran M5-May-09 3:44 
AnswerRe: Convert string to Font Pin
khodadadeh1-Sep-10 14:13
khodadadeh1-Sep-10 14:13 

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.