在文本文件中循环以构造查询并在中继器中显示结果

本文关键字:结果 中继器 查询 显示 文本 文件 循环 | 更新日期: 2023-09-27 18:25:03

我正试图循环浏览一个文本文件,并对每一行的内容运行查询。每个查询的结果数据应显示在My_Repater中。

我已经完成了以下代码,但它只使用上次查询的数据填充中继器。如何使用每个查询的结果逐行填充中继器?如有任何帮助,我们将不胜感激。

protected void txtUpload1_Click(object sender, EventArgs e)
    {
        //Define file upload control as fu
        FileUpload fu = FileUpload1;
        //Check to see if file is present
        if (fu.HasFile)
        {
            //Create StreamReader and pass contents of file
            StreamReader reader = new StreamReader(fu.FileContent);
            //Open SQL connection
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["mike_db"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            con.Open();
            //Do the following until all file content has been read
            do
            {
                string item = reader.ReadLine();
                string qry = "select * from prod_class where item =" + "'" + item + "';";
                cmd = new SqlCommand(qry, con);
                queryText.Text = qry;
                DataSet ds = new DataSet();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                //Bind data to the repeater
                My_Repeater.DataSource = ds;
                My_Repeater.DataBind(); 
            }
            while (reader.Peek() != -1);
            reader.Close();
        }
    }

在文本文件中循环以构造查询并在中继器中显示结果

您可以使用所有id运行一个查询(下面的示例)。改进代码的几点意见:

1) 首先也是最重要的一点是,您让自己容易受到SQL注入攻击。

2) 学习在一次性对象(如SQLConnection)上使用using语句

3) 存储过程可以提高性能,然后可以在表值参数中传递所有id。

protected void txtUpload1_Click(object sender, EventArgs e)
{
    //Define file upload control as fu
    FileUpload fu = FileUpload1;
    //Check to see if file is present
    if (fu.HasFile)
    {
        //Create StreamReader and pass contents of file
        StreamReader reader = new StreamReader(fu.FileContent);
        //Open SQL connection
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["mike_db"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        string items = "";
        //Do the following until all file content has been read
        do
        {
            items += reader.ReadLine() + ",";
        }
        while (reader.Peek() != -1);
        //remove last comma
        items = items.Substring(0, items.Length -1);
        string qry = String.Format("select * from prod_class where item IN ({0})", items);
        cmd = new SqlCommand(qry, con);
        queryText.Text = qry;
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        //Bind data to the repeater
        My_Repeater.DataSource = ds;
        My_Repeater.DataBind(); 
        reader.Close();
    }
}

创建一个对象来保存数据,将其填充到while循环中,然后在完成所有操作后DataBind()中继器。

public class MyObject
{
    public string MyValue1 { get; set; }
    public string MyValue2 { get; set; }
}
var myList = new List<MyObject>();
while(){
//...
myList.Add(new MyObject { MyValue1 = ds["myvalue1]", MyValue2 = ds["myvalue2"]});
}
My_Repeater.DataSource = myList;
My_Repeater.DataBind();