什么是参数传递到SQL,为什么我需要它? [英] What is passing parameters to SQL and why do I need it?

查看:124
本文介绍了什么是参数传递到SQL,为什么我需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初​​学者在这里:

在此<一个href=\"http://stackoverflow.com/questions/3077770/how-do-i-execute-an-insert-using-sqlcommand\">answer我如何将数据插入他提到传递参数,而不是字符串连接像我现在有SQL Server的问题。

这是出于安全真的有必要吗?如果是这样,究竟是什么参数传递?当我谷歌,我得到了很多有关存储过程。难道这就是我想要的,我不知道有关存储过程....呢。

如果你能指出我在正确的方向,我会AP preciate这一点。

感谢。

编辑:

好吧,这里是我得到了什么。它似乎正确地更新数据库,最终我将改变硬codeD整数从标签输入。请证实,如果我是如何做到这不是容易受到任何SQL注入或黑客。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Web.Security;使用System.Data这;
使用System.Data.Sql;
使用System.Data.SqlClient的;公共部分类统计:System.Web.UI.Page
{    公共SqlDataReader的DataReader的;
    公众的SqlCommand命令;
    字符串的queryString =(INSERT INTO的UserData(UserProfileID,ConfidenceLevel,LoveLevel,HappinessLevel)VALUES(@UID,@CL,@LL,@HL););
    //字符串的queryString =(INSERT INTO的UserData(UserProfileID,ConfidenceLevel,LoveLevel,HappinessLevel)VALUES('a051fc1b-4f51-485b-a07d-0f378528974e',2,2,2););    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
       LabelUserID.Text = Membership.GetUser()ProviderUserKey.ToString()。    }    保护无效的button1_Click(对象发件人,EventArgs的发送)
    {        //连接到数据库
        的MySqlConnection数据库=新的MySqlConnection();
        database.CreateConn();        //创建命令对象
        命令=新的SqlCommand(的queryString,database.Connection);        //添加参数。使用prevent SQL注入
        Command.Parameters.Add(@ UID,SqlDbType.UniqueIdentifier);
        Command.Parameters [@ UID]值= Membership.GetUser()ProviderUserKey。;        Command.Parameters.Add(@ CL,SqlDbType.Int);
        Command.Parameters [@ CL]值= 9。        Command.Parameters.Add(@ LL,SqlDbType.Int);
        Command.Parameters [@ LL]值= 9。        Command.Parameters.Add(@ HL,SqlDbType.Int);
        Command.Parameters [@ HL]值= 9。        Command.ExecuteNonQuery();
    }}


解决方案

将参数传递给SQL不必建立动态SQL字符串可以节省你。

建设动态SQL语句是一个巨大的安全风险,因为人们可以注入自己的SQL code到应用程序中,可能对执行数据不可取的命令。

有可能的SQL注入攻击一些很好的样本,时间:

通过实例 SQL注入攻击

有参数传递到SQL语句的方法有两种。一种是像你提到使用存储过程。另一种是使用参数化查询(这其实是我preFER)。

一个参数化查询其实很简单.NET中:

 使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{
    的SqlCommand命令=
        新的SqlCommand(SELECT * FROM用户其中username = @用户名,康恩);    command.Parameters.Add(新的SqlParameter(@用户名,贾斯汀Niessner));    SqlDataAdapter的适配器=新SqlDataAdapter的(命令);
    DataTable的DT =新的DataTable();    adapter.Fill(DT);
}

在该示例中,参数为 @username ,我们使用了<$ C $的参数集合C>的SqlCommand 对象的值传递。

Beginner here:

In this answer to my question of how to insert data into SQL Server he mentioned passing parameters instead of string concatenation like I currently have.

Is this really necessary for security? If so, what exactly is passing parameters? When i google it I get a lot about stored procedures. Is that what I want, I do not know about stored procedures....yet.

If you can point me in the right direction, I would appreciate that.

Thanks.

EDIT:

Ok, here is what I got. It seems to update the database correctly and eventually I will change the hard coded ints to inputs from a label. Please confirm if how I did this is not vulnerable to any sql injection or hacks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;



public partial class Stats : System.Web.UI.Page
{

    public SqlDataReader DataReader;
    public SqlCommand Command;
    string queryString = ("INSERT INTO UserData (UserProfileID, ConfidenceLevel, LoveLevel, HappinessLevel) VALUES (@UID, @CL, @LL, @HL);");
    //string queryString = ("INSERT INTO UserData (UserProfileID, ConfidenceLevel, LoveLevel, HappinessLevel) VALUES ('a051fc1b-4f51-485b-a07d-0f378528974e', 2, 2, 2);"); 

    protected void Page_Load(object sender, EventArgs e)
    {
       LabelUserID.Text = Membership.GetUser().ProviderUserKey.ToString();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        //connect to database
        MySqlConnection database = new MySqlConnection();
        database.CreateConn(); 

        //create command object
        Command = new SqlCommand(queryString, database.Connection);

        //add parameters. used to prevent sql injection
        Command.Parameters.Add("@UID", SqlDbType.UniqueIdentifier);
        Command.Parameters["@UID"].Value = Membership.GetUser().ProviderUserKey;

        Command.Parameters.Add("@CL", SqlDbType.Int);
        Command.Parameters["@CL"].Value = 9;

        Command.Parameters.Add("@LL", SqlDbType.Int);
        Command.Parameters["@LL"].Value = 9;

        Command.Parameters.Add("@HL", SqlDbType.Int);
        Command.Parameters["@HL"].Value = 9;

        Command.ExecuteNonQuery(); 


    }

}

解决方案

Passing parameters to SQL saves you from having to build a dynamic SQL string.

Building dynamic SQL statements is a HUGE security risk because people can inject their own SQL code into your application, possibly executing undesirable commands against your data.

There are some good samples of possible SQL Injection attacks at:

SQL Injection Attacks by Example

There are two ways of passing parameters to SQL statements. One is to use Stored Procedures like you mentioned. The other is to use parameterized queries (which is actually what I prefer).

A parameterized query is actually quite easy in .NET:

using(SqlConnection conn = new SqlConnection(connString))
{
    SqlCommand command = 
        new SqlCommand("SELECT * FROM Users WHERE Username = @Username", conn);

    command.Parameters.Add(new SqlParameter("@Username", "Justin Niessner"));

    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataTable dt = new DataTable();

    adapter.Fill(dt);
}

In that example, the parameter was @Username and we used the Parameters collection of the SqlCommand object to pass in the value.

这篇关于什么是参数传递到SQL,为什么我需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆