【问题标题】:SQL Command not filling data readerSQL 命令未填充数据读取器
【发布时间】:2012-10-15 08:58:50
【问题描述】:

我正在开发一个嵌套中继器。目前我的问题似乎是,当我执行我的 SQL 命令时,没有数据返回给数据读取器。即使我在 SQL Server 中运行完全相同的查询(复制和粘贴)。

我的 noteDrClient 阅读器不包含数据,但它知道表中有 5 列。我不知道此时该做什么,也不知道为什么没有数据被传递到数据读取器中。谁能看出明显的问题?

  SqlConnection con = new SqlConnection("Data Source=***;Initial Catalog=*;User   ID=*;Password=*;Integrated Security=False;MultipleActiveResultSets=true;");

上面是我的连接字符串。请注意,我将多个活动结果集设置为 true。我这样做是因为我一直收到关于我的数据阅读器打开的错误,即使它已关闭。

   protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;
        if ((item.ItemType == ListItemType.Item) ||
            (item.ItemType == ListItemType.AlternatingItem))
        {

            System.Data.Common.DbDataRecord rd = (System.Data.Common.DbDataRecord)e.Item.DataItem;

            Repeater nestedRepeater = e.Item.FindControl("NotesRepeater") as Repeater;
            string FID = rd[0].ToString();


            using (cmd2 = new SqlCommand("SELECT * FROM notes WHERE FID = 1356;", con)) ;

            SqlDataReader noteDrClient = cmd2.ExecuteReader();  //no data is being filled to the data reader... even though this command pulls data in SQL Server Management Studio.

            if (noteDrClient.Read()) {   //bind the repeater if there is data to bind
                    nestedRepeater.DataSource = noteDrClient;
                    nestedRepeater.DataBind();
            }

            noteDrClient.Close();                                   




        }

【问题讨论】:

标签: c# asp.net sql


【解决方案1】:

您正在使用的语句是在您有机会使用 SqlCommand 之前处理它。此外,您正在尝试绑定到 DataReader。将数据读取器的结果获取到“Note”实体的集合中,然后绑定到该集合。

        using (SqlCommand cmd2 = new SqlCommand("SELECT * FROM notes WHERE FID = 1356;", con))
        {

            using(SqlDataReader noteDrClient = cmd2.ExecuteReader())
            {

                while (noteDrClient.Read()) 
                {   
                    Note n = new Note();
                    ... get note from data reader
                    notes.Add(n); // add note to collection
                }
            }
        } 

        // bind child
        nestedRepeater.DataSource = notes;       
        nestedRepeater.DataBind();       

编辑:

您可能想查看 DataAdapter - http://www.mikesdotnetting.com/Article/57/Displaying-One-To-Many-Relationships-with-Nested-Repeaters

【讨论】:

    【解决方案2】:

    我通过创建一个额外的连接字符串而不是重复使用我一直用于主中继器的相同连接字符串解决了这个问题。数据仍然没有约束力,但它确实存在。

                using (cmd2 = new SqlCommand("SELECT * FROM notes WHERE FID = 1356;", con2)) ;
    

    【讨论】:

      【解决方案3】:

      我认为您查询中的分号可能会导致问题。

      尝试在值周围使用引号,如下所示:

      SELECT * FROM notes WHERE FID = '1356;'

      如果分号不是值的一部分:

      SELECT * FROM notes WHERE FID = '1356'

      【讨论】:

      • 分号表示 SQL 语句的结束。如果 FID 的数据类型是 INT,则不需要引号,但分号有效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多