English 中文(简体)
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 that 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

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签