从 SQL Server 到标签的文本

发布于 2024-12-02 23:31:12 字数 74 浏览 0 评论 0原文

如何从 SQL Server 中选择一列并将其放入标签中,然后标签始终显示最新的列(我有 Datetime

How do I select one column from my SQL Server and put it into a label, and then the label always shows the newest (I have Datetime)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦与时光遇 2024-12-09 23:31:12

这是一篇值得您阅读并遵循的好文章。它有 4 个部分。您将在那里找到答案并学习基础知识。

http://weblogs .asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

This is a good article for you to read through and follow. There's 4 parts to it. You'll find your answers in there plus learn the basics.

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

坦然微笑 2024-12-09 23:31:12

我想说你应该首先学习所有内容的基础知识,但我会在这里给你一个指导。

假设你的表格看起来像这样

CREATE TABLE MyTable
(
    DateSaved datetime,
    MyLabel varchar(25)
)

,所以你有一个日期时间字段和一个标签字段。您现在需要一个存储过程来从 SQL Server 中获取这些数据。

CREATE PROCEDURE GetMyLabel
AS

/* Order it so that your dates are descending, meaning newest rows are first. */
select top 1 MyLabel from MyTable
order by DateSaved desc
GO

/* Grant rights to a user for your SqlConnection later.  I'll just call it WebUser. */
GRANT EXECUTE TO WebUser ON GetMyLabel
GO

现在您的 SQL 已完成。现在是时候编码了。我假设您使用的是 C# (.NET)。

using System.Data;
using System.Data.SqlClient;

....

protected void Page_Load(object sender, EventArgs e)
{
   string myLabelText = Get_MyLabel;

   if (myLabelText != null)
      MyLabel.Text = myLabelText;
}

private void Get_MyLabel(string connStr)
{
    string myLabelText;

    try
    {
        conn.Open();

        // Returns a single column from the first row of the query
        string myLabelText = cmd.ExecuteScalar().ToString();                
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex);
    }
    finally
    {
        if (conn != null && conn.State != ConnectionState.Closed)
            conn.Close()
    }

    return myLabelText;
}

这样就可以了。祝你好运!

I'd say you should go learn the basics on everything first, but I'll give you a pointer here..

Let's say your table looks like this

CREATE TABLE MyTable
(
    DateSaved datetime,
    MyLabel varchar(25)
)

So you have one datetime field and one field for your label. You now need a stored procedure to get this data out of your sql server so.

CREATE PROCEDURE GetMyLabel
AS

/* Order it so that your dates are descending, meaning newest rows are first. */
select top 1 MyLabel from MyTable
order by DateSaved desc
GO

/* Grant rights to a user for your SqlConnection later.  I'll just call it WebUser. */
GRANT EXECUTE TO WebUser ON GetMyLabel
GO

Now your SQL is done. So now time for your coding. I'll assume you are using c# (.NET).

using System.Data;
using System.Data.SqlClient;

....

protected void Page_Load(object sender, EventArgs e)
{
   string myLabelText = Get_MyLabel;

   if (myLabelText != null)
      MyLabel.Text = myLabelText;
}

private void Get_MyLabel(string connStr)
{
    string myLabelText;

    try
    {
        conn.Open();

        // Returns a single column from the first row of the query
        string myLabelText = cmd.ExecuteScalar().ToString();                
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex);
    }
    finally
    {
        if (conn != null && conn.State != ConnectionState.Closed)
            conn.Close()
    }

    return myLabelText;
}

and that should do it. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文