Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 此方法返回的数据表是否安全?_C# 4.0 - Fatal编程技术网

C# 4.0 此方法返回的数据表是否安全?

C# 4.0 此方法返回的数据表是否安全?,c#-4.0,C# 4.0,我有以下功能: public DataTable GetRecordSet(String query, string[] parameters) 它通常使用如下: DataTable dt = GetRecordSet( "select * from objects where ob_id = @obid and ac_id = @acid", new[] { objectId, accountId }); SqlDataAdapter da = new SqlDataAdapter();

我有以下功能:

public DataTable GetRecordSet(String query, string[] parameters)
它通常使用如下:

DataTable dt = GetRecordSet( "select * from objects where ob_id = @obid and ac_id = @acid", new[] { objectId, accountId });
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = GetSqlCommand(query, parameters);
GetRecordSet方法解析查询参数,查找@前面的单词,并创建适当的SqlParameter对象,该对象被传递到SqlCommand对象中。在这种情况下,将创建两个参数

查询参数将始终由我提供,参数数组可能包含来自用户的数据

我担心的是,因为我是从字符串生成参数名的,所以我可能无意中规避了反SQL注入对策。这种方法安全吗

更新: GetRecordSet获取一个select命令,如下所示:

DataTable dt = GetRecordSet( "select * from objects where ob_id = @obid and ac_id = @acid", new[] { objectId, accountId });
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = GetSqlCommand(query, parameters);
GetSqlCommand方法:

private SqlCommand GetSqlCommand(string query, string[] parameters)
{
    SqlCommand sq = new SqlCommand(query, myConnection);

    // enum parameters
    // get name of parameter from the query string and add it to the SqlCommand
    SqlParameter p;
    string[] paramNames = query.Split('@');
    string name;

    int paramCounter = 0;
    if (parameters != null)
    {
        foreach (string param in parameters)
        {
            paramCounter += 1;
            if (param != "")
            {
                try
                {
                    if (paramNames.Length > paramCounter)
                    {
                        name = paramNames[paramCounter].Split(' ')[0];

                        p = new SqlParameter(name, param);
                        sq.Parameters.Add(p);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: {0}", ex);
                }
            }
        }
    }
    return sq;
}

上面的代码不受SQL注入的影响-有一种方法可以利用,但这段代码不受特定问题的影响


当然,没有办法100%确定,但这已经是最好的了。

上面的代码不受SQL注入的影响-有一种方法可以利用,但这段代码不受特定问题的影响


当然,没有办法100%确定,但这是最好的办法。

没有。从外部世界传递参数的唯一方法是如果已显式 确定的或正确引用的。只在其中插入任何文本块都是不安全的
查询。

否。从外部世界传递参数的唯一方法是如果已显式 确定的或正确引用的。只在其中插入任何文本块都是不安全的
查询。

如果不查看
GetRecordSet
,很难说。虽然它使用了参数,但应该是安全的。更新了问题的更多细节。如果没有看到
GetRecordSet
,很难说。虽然它使用了参数,但应该是安全的。更新了问题的更多细节。