【问题标题】:getting news from DB从 DB 获取消息
【发布时间】:2011-04-05 10:28:48
【问题描述】:
public partial class newsarticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int article_id = Convert.ToInt32(Request.QueryString["id"]);

            string select = "SELECT [ID],[NEWS],[CONTENTS] FROM [NEWS]";
            string strCon = System.Web
                      .Configuration
                      .WebConfigurationManager
                      .ConnectionStrings["ConnectionString"]
                      .ConnectionString;

            SqlConnection conn = new SqlConnection(strCon);
            conn.ConnectionString = strCon;
            conn.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand(select, conn);
            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                news_title.Text = myReader["NEWS"].ToString();
                news_content.Text = myReader["CONTENTS"].ToString();

            }
            conn.Close();
        }
    }

你好,我对这种方法有疑问。我将 DB 中的这些“新闻”和“内容”与 news_title 和 news_content 连接起来,在 newsarticle.aspx 站点中,我有两个用于编写新闻和内容的文字控件。我有

<a href="/newsarticle.aspx?id=<%#Eval("ID") %>"> 
   <asp:Label ID="lblTitle" runat="server" Text='<%#Eval("News") %>'>
   </asp:Label>
</a>

, 有了这个,我得到了新闻名称的链接,当我点击一个链接时,它总是返回我的姓氏和来自表的内容。例如。如果我有 4 个赞,当我点击 2 时,我会从 4 个链接获得结果。 有人知道有什么办法吗?

【问题讨论】:

    标签: .net asp.net sql


    【解决方案1】:

    您尚未在 select 语句的 where 子句中添加 article_id

    string select = "SELECT [ID],[NEWS],[CONTENTS] FROM [NEWS] where ID = " + article_id.ToString();
    

    【讨论】:

    • 你能不能调试一下,看看你这里有没有得到id int article_id = Convert.ToInt32(Request.QueryString["id"]);你需要在调试模式下检查这个变量 article_id
    • 这是我的Skype id toakhtar,加我然后我会看到你的问题并尝试解决
    猜你喜欢
    • 2013-07-19
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多