2013-08-20 16 views
-1

在我的主頁上我讓我Visual Studio我想在我的首頁上有一些歡迎文本。在文本中添加來自DB(Select Query)的值。 Asp.net

事情是,我想在文本中間呈現一些值,我從我的數據庫中抓取一個選擇查詢。例如:

可以說例如:

歡迎來到我的主頁!

當前有(Select Count(UserID) FROM users where Status = 'active')我的頁面上活動的用戶和(Select Count(UserID) FROM users where Status = 'inactive')處於非活動狀態。

我真的很陌生,但不知何故,我需要在Page_load上運行我的問題,然後能夠將這個值呈現在標籤或其他東西中?

感謝您的幫助

回答

0

使用ADO.NET:

1:創建一個connection對象,設置它的connection string財產

2:創建一個command對象及其text屬性設置爲

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

3:使用ExecuteScalar命令對象的方法找到你想要的結果並設置標籤text屬性等於該結果。

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>處於非活動狀態。 如果你覺得這很有用,它解決了你的問題,將其標記爲答案並放棄。

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(); 
    } 
相關問題