C#SqlDataReader写入多行

时间:2013-11-18 09:29:05

标签: c# sql-server sqldatareader

我想知道如何写出我的行,而不仅仅是第一行。

这是我的c#

string SessionID = Session["ID"].ToString();

using (SqlConnection connection = new SqlConnection(@"Data Source=JENSKVIST\SQLEXPRESS;Initial Catalog=BlogNetwork;Integrated Security=True"))
{
    connection.Open();

    string query = "SELECT * FROM Entries ORDER BY Entry_ID DESC";
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                string DataHeadline = reader["Entry_Headline"].ToString();
                string DataUserID = reader["Entry_UserID"].ToString();
                string DataMedia = reader["Entry_Media"].ToString();
                string DataContent = reader["Entry_Content"].ToString();
                string DataID = reader["Entry_ID"].ToString();

                User_ID.Value = DataID;
                Entry_Headline.Text = DataHeadline;
                Entry_Media.ImageUrl = DataMedia;
                Entry_Content.Text = DataContent;
            }
        }

    }

    string UserQuery = "SELECT * FROM Users WHERE User_ID=@DataID";
    using (SqlCommand UserCommand = new SqlCommand(UserQuery, connection))
    {

        UserCommand.Parameters.AddWithValue("@DataID", User_ID.Value);

        using (SqlDataReader UserReader = UserCommand.ExecuteReader())
        {
            while (UserReader.Read())
            {
                string DataAvatar = UserReader["User_Avatar"].ToString();
                string DataName = UserReader["User_Firstname"].ToString() + " " + UserReader["User_Lastname"].ToString();

                Entry_Avatar.ImageUrl = DataAvatar;
                Entry_Name.Text = DataName;

            }
        }
    }
}

这是我的asp元素

<asp:HiddenField runat="server" ID="User_ID" />

<asp:Image runat="server" ID="Entry_Avatar" />

<asp:Label runat="server" ID="Entry_Name" /> posted entry <asp:Label runat="server" ID="Entry_Headline" />

<asp:Image runat="server" ID="Entry_Media" Width="400" />

<asp:Label runat="server" ID="Entry_Content" />... <asp:HyperLink runat="server" ID="Entry_Link">Read more</asp:HyperLink>

我不知道怎么做,我也在线搜索,但没有找到解决方案。如果你们想看看我的sql表,请随时问。我希望你们能帮助我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

以下是我创建的示例应用程序。

entitySet是一个集合。

           using (_connection)
            {
                reader = command.ExecuteReader();

                fieldCount = reader.FieldCount;
                properties = new SortedList<string, PropertyInfo>();

                foreach (PropertyInfo pi in typeof(T).GetProperties())
                {
                    properties.Add(pi.Name.ToUpper(), pi);
                }

                while (reader.Read())
                {
                    T item = Activator.CreateInstance<T>();

                    for (int i = 0; i < fieldCount; i++)
                    {
                        if (reader[i] != DBNull.Value)
                            properties[reader.GetName(i).ToUpper()].SetValue(item, reader[i], null);
                    }

                    entitySet.Add(item);
                }
            }
            reader.Close();