-1

在我的主页上,我制作了Visual Studio,我想在我的首页上有一些欢迎文字。

问题是我希望在文本中间显示一些我通过选择查询从我的数据库中获取的值。

比如说:

欢迎来到我的主页!

目前(Select Count(UserID) FROM users where Status = 'active')在我的页面上有活跃的用户并且(Select Count(UserID) FROM users where Status = 'inactive')是非活跃的。

我对此真的很陌生,但不知何故我需要在 Page_load 上运行我的问题,然后能够获取该值并将其呈现在标签或其他东西中?

谢谢你的帮助

4

3 回答 3

0

使用 ADO.NET:

1:创建一个connection对象,设置它的connection string属性

2:创建一个command对象并将其text属性设置为

Select Count(UserID) FROM users where Status = 'active'

3:使用ExecuteScalar命令对象的方法找到你想要的结果并将标签text属性设置为等于该结果。

于 2013-08-20T12:37:52.333 回答
0

程序

create procedure select
@activity
as
Begin
 Select Count(UserID) FROM users where Status = @activity
end

C# 页面加载

string activity1="active";
string activity2="Inactive";
sqlconnection con=new sqlconnection("give your connection string");
sqlcommand cmd=new sqlcommand();
cmd=new sqlcommand("select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@activity",activity1);
String result1=cmd.ExecuteNonQuery();

sqlcommand cmd1=new sqlcommand();
cmd1=new sqlcommand("select",con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@activity",activity2);
String result2=cmd.ExecuteNonQuery();

Label1.Text=result1;
Label2.Text=result2;

.aspx 页面

欢迎来到我的主页 目前<asp:label id="Label1" runat="server"></asp:Label>在我的页面上<asp:label id="Label2" runat="server"></asp:Label>有活跃的用户和不活跃的用户。如果您发现这很有用并且它解决了您的问题,请将其标记为答案并放弃投票。

于 2013-08-20T12:51:36.897 回答
0

尝试这个

 if (!IsPostBack)
    {
        SqlConnection con = new SqlConnection(@"MY CONNECTION");
        SqlCommand com = new SqlCommand("Select Count(UserID) FROM users where Status = 'active'", con);
        SqlDataReader dr;
        con.Open();
        while (dr.Read())
        {
            Debug.WriteLine(dr[0].ToString());
        }
        con.Close();
    }
于 2013-08-20T13:08:40.537 回答