【问题标题】:Display specific data on asp.net webpage from a microsoft sql express data table在 microsoft sql express 数据表中显示 asp.net 网页上的特定数据
【发布时间】:2009-11-19 17:38:41
【问题描述】:

我一直在互联网上寻找一种从 sql 数据表中显示特定内容的方法。我希望我可以根据 Id 列值显示 Content 列。 alt text http://photos-h.ak.fbcdn.net/hphotos-ak-snc3/hs031.snc3/11852_1241994617732_1465331687_655971_2468696_n.jpg

【问题讨论】:

  • 链接返回 404 错误
  • 有什么问题?图片在哪里?
  • 你可能想看看一些关于 asp.net 和数据绑定的教程learnvisualstudio.net/content/series/…
  • 可能是一个重复的问题,但却是一个真实的问题。我没有得到反对票。
  • 而且我不知道副本是从哪里来的。

标签: asp.net sql-server datatable


【解决方案1】:

如果你想从一个记录中得到一个值,你可以使用SqlCommand类的ExecuteScalar方法:

string title = null;
using (SqlConnection conn = new SqlConnection("your-connection-string"))
using (SqlCommand cmd = new SqlCommand(
    "select ContentTitle from {put table name here} where id = 4", conn))
{
    conn.Open();
    title = (string)conn.ExecuteScalar();
}

if (!string.IsNullOrEmpty(title))
{
    // assign title to suitable asp.net control property
}

如果您希望能够为各种 id 执行此操作,不要只是连接一个新的 sql 字符串。我再重复一遍:不要只是连接一个新的 sql 字符串。改用参数:

string title = null;
using (SqlConnection conn = new SqlConnection("your-connection-string"))
using (SqlCommand cmd = new SqlCommand(
    "select ContentTitle from {put table name here} where id = @id", conn))
{
    SqlParameter param  = new SqlParameter();
    param.ParameterName = "@id";
    param.Value = yourIdGoesHere;
    cmd.Parameters.Add(param);

    conn.Open();
    title = (string)conn.ExecuteScalar();
}

if (!string.IsNullOrEmpty(title))
{
    // assign title to suitable asp.net control property
}

更新
示例 aspx 页面。首先是一些标记(假设文件名为 example.aspx):

<body>
   <form id="Form1" runat="server">
      Title: <asp:Label id="_titleLabel" 
                 Text="{no title assigned yet}" 
                 runat="server"/>
   </form>
</body>

...在代码隐藏中(称为 example.aspx.cs;为简单起见,我只包含了 Page_Load 事件):

protected void Page_Load(object sender, EventArgs e)
{
    int id;
    try
    {
        if (int.TryParse(Request.QueryString["id"], out id))
        {
            _titleLabel.Text = GetContentTitle(id);
        }
        else
        {
            _titleLabel.Text = "no id given; cannot look up title";
        }
    }
    catch (Exception ex)
    {
        // do something with the exception info
    }
}

private static string GetContentTitle(int id)
{ 
    using (SqlConnection conn = new SqlConnection("your-connection-string"))
    using (SqlCommand cmd = new SqlCommand(
        "select ContentTitle from {put table name here} where id = @id", conn))
    {
        SqlParameter param  = new SqlParameter();
        param.ParameterName = "@id";
        param.Value = yourIdGoesHere;
        cmd.Parameters.Add(param);

        conn.Open();
        return (string)conn.ExecuteScalar();
    }
}

免责声明:代码直接写入答案窗口,未经测试(我目前无法访问开发环境),因此可能存在错误

【讨论】:

  • 我相信这行得通。我只是不知道如何在 example.aspx 网页中实现它。请发布asp.net代码的示例。
  • 哦,我把你展示的第一段CS代码放在什么文件里。我只有 15 岁,我正在努力学习 asp.net。
  • 谢谢。有机会我会试一试的。
  • 我在“your-connection-string”处插入什么代码。感谢您的所有时间。
  • 连接字符串取决于您的数据库。这是连接字符串的好资源:connectionstrings.com
猜你喜欢
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多