开发者

Display specific data on asp.net webpage from a microsoft sql express data table

I have been looking around the internet for a way to display specific content from a sql data table. I was hoping tha开发者_JAVA技巧t I could display the Content column according to the Id column value. alt text http://photos-h.ak.fbcdn.net/hphotos-ak-snc3/hs031.snc3/11852_1241994617732_1465331687_655971_2468696_n.jpg


If you want to have exactly one value from one single record, you can use the ExecuteScalar method of the SqlCommand class:

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
}

If you want to be able to do this for various ids, do not just concatenate a new sql string. I will repeat that: do not just concatenate a new sql string. Use parameters instead:

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
}

Update
Sample aspx page. First some markup (let's say the file is called example.aspx):

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

...and in the code-behind (that would be called example.aspx.cs; I have included only the Page_Load event for simplicity):

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();
    }
}

Disclaimer: the code is written directly into the answer window and not tested (I don't have access to a development environment right now) so there may be errors

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜