我试图从我的数据库中显示一个表的计数.我有一个返回行数的函数.我是c#的新手,我不知道如何在我的html中调用该函数,以便显示返回的值.
这是我从中获取的c#函数如何计算c#中sql表的行数?:
public int Get_Count(object sender, EventArgs e)
{
{
string stmt = "SELECT COUNT(*) FROM dbo.listing";
int count = 0;
using (SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE"))
{
using (SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
{
thisConnection.Open();
count = (int)cmdCount.ExecuteScalar();
}
}
return count;
}
}
Run Code Online (Sandbox Code Playgroud)
这是HTML行,我想显示返回的计数:
<p class="lead text-muted block text-center"><strong>Get_Count()</strong> results found</p>
Run Code Online (Sandbox Code Playgroud)
在页面加载事件中
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
public int Get_Count()
{
{
string stmt = "SELECT COUNT(*) FROM dbo.listing";
int count = 0;
using (SqlConnection thisConnection = new SqlConnection("Data Source=DATASOURCE"))
{
using (SqlCommand cmdCount = new SqlCommand(stmt, thisConnection))
{
thisConnection.Open();
count = (int)cmdCount.ExecuteScalar();
}
}
return count;
}
}
Run Code Online (Sandbox Code Playgroud)
然后
<p class="lead text-muted block text-center"><strong><%# Get_Count() %></strong> results found</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1413 次 |
| 最近记录: |