Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.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# 在ASP.NET(C)中填充GridView时出现问题_C#_.net_Asp.net_Sql_Gridview - Fatal编程技术网

C# 在ASP.NET(C)中填充GridView时出现问题

C# 在ASP.NET(C)中填充GridView时出现问题,c#,.net,asp.net,sql,gridview,C#,.net,Asp.net,Sql,Gridview,我试图用循环的结果填充Gridview。但我只得到循环中的最后一个结果。 我认为每次执行for循环时,GridView都会被覆盖 你们能帮我解决这个问题吗 for (int j = 0; j < i; j++) { Label1.Text += fipath[j]; Label1.Text += "-------------"; SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Databa

我试图用循环的结果填充Gridview。但我只得到循环中的最后一个结果。 我认为每次执行for循环时,GridView都会被覆盖

你们能帮我解决这个问题吗

for (int j = 0; j < i; j++)
{
    Label1.Text += fipath[j];
    Label1.Text += "-------------";
    SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
    SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='" + NAME + "' AND FilePath='" + fipath[j] + "'", conn);

    try
    {
        conn.Open();
        SqlDataReader rdr = comm.ExecuteReader();
        if (Role.Equals("admin"))
        {
            GridView1.DataSource = rdr;
            GridView1.DataBind();
        }
        rdr.Close();
    }
    catch
    {
        conn.Close();
    }
}

在循环外部创建列表或BindingSource,将其绑定到gridview,然后将所有记录添加到该列表或源


当前方法的问题是,每次都用新的数据源覆盖从数据库中提取的记录,因此,如您所述,只设置最后一个记录,并处理旧的分配。

此代码有多个问题:

似乎如果Role==admin,您根本不需要查询数据库 网格的数据源在每次循环迭代时都被覆盖,这就是为什么您只看到最后一个值。 使用SqlCommand的参数可防止SQL注入。 不要在循环中运行字符串连接。改用StringBuilder 用于您的连接。这样代码就更干净了。 修复可能如下所示:

if (Role != "admin")
    return;

var dataTable = new DataTable();
var stringBuilder = new StringBuilder();
using (var connection = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true"))
using (var command = connection.CreateCommand())
{
    connection.Open();
    command.CommandText = "Select * from FileUpload where UploadedBy = @UploadedBy AND FilePath = @FilePath";
    command.Parameters.AddWithValue("UploadedBy", NAME);
    var filPathParameter = command.Parameters.Add("FilePath", SqlDbType.VarChar);
    for (int j = 0; j < i; j++)
    {
        stringBuilder.Append(fipath[j]);
        stringBuilder.Append("-------------");
        filPathParameter.Value = fipath[j];
        dataTable.Load(command.ExecuteReader(), LoadOption.PreserveChanges);
    }
}
Label1.Text += stringBuilder.ToString();
GridView1.DataSource = dataTable;
GridView1.DataBind();
此查询易于SQL注入。并且在MS SQL中有2100个元素的限制

实现这一点的方法不止一种。取决于您的DBMS和要求。

在SQL查询中使用in子句,并在FilePath中传递ID列表

SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='" + NAME 
 + "' AND FilePath in (" + listOfIDs + ")", conn);
查看这些与in子句的使用相关的URL


如何将项目动态添加到列表中。请演示给我看。@7590:使用rdr实例中的Read方法,并使用该方法将对象添加到bindingsource实例中。它看起来是正确的。但由于某些原因,它没有给出任何结果。我删除了for循环,并给出了fipath,而不是fipath[j].它什么也没打印。你必须给出参数中的Id列表,如。。。在1,2,3,4中,如果您有SQL server,只需在那里验证查询并在SQL命令中复制该查询。从文件上载中选择*其中uploadedby=和文件路径1,3,4,5
SqlCommand comm = new SqlCommand("Select * from FileUpload where UploadedBy='" + NAME 
 + "' AND FilePath in (" + listOfIDs + ")", conn);