Sanitizing SQL data

后端 未结 2 753
我寻月下人不归
我寻月下人不归 2021-01-04 18:13

Google turns up all sorts of discussions on sanitizing queries for web access but I\'m not finding anything addressing what I\'m concerned with:

Sanitizing user inpu

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 18:46

    Use a properly constructed DAL with SQL Parameter objects handed to stored procedures and you don't have to worry about this. Implement business objects and dal to abstract the user input enough that it isn't executed as SQL but rather recognized as values. examples are fun:

    public class SomeDal
    {
        public void CreateUser(User userToBeCreated)
        {
            using(connection bla bla)
            {
                // create and execute a command object filling its parameters with data from the User object
            }
        }
    }
    
    public class User
    {
        public string Name { get; set; }
        ...
    }
    
    public class UserBL
    {
        public CreateUser(User userToBeCreated)
        {
            SomeDal myDal = new SomeDal();
            myDal.CreateUser(userToBeCreated);
        }
    }
    
    public class SomeUI
    {
        public void HandleCreateClick(object sender, e ButtonClickEventArgs)
        {
            User userToBeCreated = new User() { Name = txtName.Text };
            UserBL userBl = new UserBL();
            userBl.CreateUser(userToBeCreated);
        }
    }
    

提交回复
热议问题