Click here to Skip to main content
15,122,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE PROCEDURE SelectAll
AS
  Declare @Quary nvarchar(1000);

  set @Quary = 'select ID,Name from tblStudents where ID = ID and Name = Name';

  exec(@Quary);

RETURN 0


What I have tried:

adding more varibles to the code but it asked me to add sacaler varible

SQL
CREATE PROCEDURE SelectAll
AS
  Declare @Quary nvarchar(1000);

  declare @ID int;

  Declare @Name Varchar(100);

  set @Quary = 'select ID,Name from tblStudents where ID = ID and Name = Name';

  exec(@Quary);

RETURN 0
Posted
Updated 25-Aug-19 4:56am
Comments
Member 14479161 30-Aug-19 19:36pm
   
hi
how about this code do it prevent from sql injection attacks

alter proc SP_SelectAll
@SQL  varchar (1000) output
as
begin
    select person_ID,FullName,Email,Date_OF_Birth,Age,GenderValue,City, [DepartementName] ,[location],[DepartementHead] ,Salary
           from Person
           join PersonGender
           on Person.Gender_ID = PersonGender.Gender_ID
           join Departements
           on Person.DepartementID = Departements.Dpartement_ID
           join DatesOfBirth 
           on Person.dateOfBirthID = DatesOfBirth.Date_ID
end

No.

SQL injection comes whenever you convert parameters to strings, and concatenate them into an SQL instruction. Always use Parameterized queries instead - and you can't use parameterised queries with EXEC.

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?
   
Comments
Member 14479161 25-Aug-19 9:14am
   
hi

i want to ask what about displaying a normal table in a aspx page whithout a form from database just displaying no input
Member 14479161 25-Aug-19 9:17am
   
i have writen it like that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Learn_Ado
{
    public partial class WebForm12 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["MSSQLDATABASE"].ConnectionString;

            using(SqlConnection con = new SqlConnection(cs))
            {
                con.Open();

                SqlDataAdapter da = new SqlDataAdapter("select * from tblStudents where Name like @Name;", con);

                da.SelectCommand.Parameters.AddWithValue("@Name",TextBox1.Text);

                DataSet ds = new DataSet();

                da.Fill(ds);

                GridView1.DataSource = ds;

                GridView1.DataBind();


            }
        }
    }
}
<pre>
OriginalGriff 25-Aug-19 10:09am
   
That's fine - the string is a parameter, so that not vulnerable.
Couple of details though:
SQL LIKE is not an exact comparison, but it requires wildcards to work.
... SqlDataAdapter("select * from tblStudents where Name like '%' + @Name + '%';", con);
Would work better: % is the SQL wildcard for "match anything".

Your DataAdapter should also be in a using block, just like your Connection.

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
Member 14479161 25-Aug-19 10:19am
   
ok got it thank you very mush
OriginalGriff 25-Aug-19 10:25am
   
You're welcome!
Quote:
Do this SQL code prevent SQL injection

Yes, because the query don't use external/user data.
But the query can be greatly simplified:
from
SQL
CREATE PROCEDURE SelectAll
AS
  Declare @Quary nvarchar(1000);

  set @Quary = 'select ID,Name from tblStudents where ID = ID and Name = Name';

  exec(@Quary);

RETURN 0

to
SQL
CREATE PROCEDURE SelectAll
AS
  Declare @Quary nvarchar(1000);

  set @Quary = 'select ID,Name from tblStudents';

  exec(@Quary);

RETURN 0

and then
SQL
CREATE PROCEDURE SelectAll
AS
select ID,Name from tblStudents;

RETURN 0


Have a look there: SQL Stored Procedures[^]
to learn how to to use parameters in a stored procedure.
   
v2

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