Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:05:51+00:00 2026-05-23T02:05:51+00:00

I am new to sql, and I try to do manipulation on data with

  • 0

I am new to sql, and I try to do manipulation on data with inline /in-code sql commands. However, I know that u need to write a lot of code sometimes in trans-sql.. When i write it,, then people say it is subject to sql injections. Then I dont know what to do…

What would be easier for me to do taking into account that i am not an expert on databases programming. Should i use Stored procedures or inline text. And how do i pass parameters to stored procedures? (e.g if i have an input from a textbox).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:05:52+00:00Added an answer on May 23, 2026 at 2:05 am

    Let’s take an example to Login a user:

    the normal query would be something like

    SELECT name, lastLogin 
    FROM [tblUsers] 
    where username = 'balexandre' AND password = 'veryhard';
    

    when putting this into code, it would be kind’a

    using (SqlConnection connection = new SqlConnection(connectionString)) { 
    
        DataSet userDataset = new DataSet(); 
    
        string query = "SELECT name, lastLogin FROM [tblUsers] where username = '" + txtUser.Text.Trim() + "' AND password = '" + txtPassword.Text.Trim() + "'";
    
        SqlDataAdapter myCommand = new SqlDataAdapter(query, connection);
        myCommand.Fill(userDataset); 
    }
    

    But this will not prevent SQL Injection, as, try to imagine what happen if I add in the username textbox something like % and in the password textbox something like ' OR 1=1-- or just ' OR 1=1-- in the username textbox. This would be valid as: 1 is a matter of fact equal to 1 and I would be logged in…

    the passed Query would be like:

    SELECT name, lastLogin 
    FROM [tblUsers] 
    where username = '' OR 1=1 --' AND password = '';
    

    You get it, right?

    But if we use parameters in our query, this will never happen, like this:

    using (SqlConnection connection = new SqlConnection(connectionString)) { 
    
        DataSet userDataset = new DataSet(); 
    
        string query = "SELECT name, lastLogin FROM [tblUsers] where username = @username AND password = @password";
        SqlDataAdapter myCommand = new SqlDataAdapter(query, connection);
    
        myCommand.SelectCommand.Parameters.Add("@username", SqlDbType.VarChar, 30); 
        myCommand.SelectCommand.Parameters["@username"].Value = txtUser.Text.Trim(); 
    
        myCommand.SelectCommand.Parameters.Add("@password", SqlDbType.VarChar, 30); 
        myCommand.SelectCommand.Parameters["@password"].Value = txtPassword.Text.Trim(); 
    
        myCommand.Fill(userDataset); 
    }
    

    Samething if you want to use Store Procedures:

    using (SqlConnection connection = new SqlConnection(connectionString)) { 
    
        DataSet userDataset = new DataSet(); 
    
        string query = "spGetUser";
        SqlDataAdapter myCommand = new SqlDataAdapter(query, connection);
        myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; 
    
        myCommand.SelectCommand.Parameters.Add("@username", SqlDbType.VarChar, 30); 
        myCommand.SelectCommand.Parameters["@username"].Value = txtUser.Text.Trim(); 
    
        myCommand.SelectCommand.Parameters.Add("@password", SqlDbType.VarChar, 30); 
        myCommand.SelectCommand.Parameters["@password"].Value = txtPassword.Text.Trim(); 
    
        myCommand.Fill(userDataset); 
    }
    

    Nowdays, you can avoid to think in all of this if you work and use something like a Database ORM (Object-relational mapping) that, instead of righting all the SQL stuff and worry about this, the Framework will take care of all known actions for you.

    One of the most used Databases ORM is NHibernate and ADO Entity Framework.

    You can use LINQ queries with this and your call would be like:

    public User GetUser(string username, string password)
    {
        return _db.Users.FirstOrDefault(x => x.User == username && x.Password == password);
    }
    

    and if you had a Store Procedure, it’s the same (the example below just shows another way to call it)

    public User GetUser(string username, string password)
    {
        return (from u in _db.spGetUser(username, password)
                select u).FirstOrDefault();                
    }
    

    And you don’t need to think about, caching, injection, concurrency, etc …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.Net application with the following code: try { sql = new
I'm fairly new to SQL and was trying to write a procedure that would
When I try to install a new instance of SQL Server 2008 Express on
When I try to create a new Maintenance Plan in SQL Server, I get
I need to create an entirely new Sql Server 2008 database and want to
Every time I try to create a new SQL Server 2005 server project I
I'm new in WPF and I'd like to visualize data from SQL query to
When I try to add a new Sql Server Database file to my Visual
I try to execute raw SQL in Grails with this code: class PlainSqlService {
I have this code to create new sql table when i execute this its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.