从c#函数获取返回值并以html显示

时间:2017-12-08 00:11:03

标签: c# html asp.net

我正在尝试显示数据库中表的计数。我有一个返回行数的函数。我是c#的新手,我不知道如何在我的html中调用该函数,以便显示返回的值。

这是我从How to count the number of rows from sql table in c#?获得的c#函数:

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;
    }
} 

这是我要显示返回计数的HTML行:

<p class="lead text-muted block text-center"><strong>Get_Count()</strong> results found</p>

1 个答案:

答案 0 :(得分:5)

页面加载事件

中的

$ie = new-object -ComObject "InternetExplorer.Application"
$url = "https://support.microsoft.com/en-us/help/4052574/cumulative-update-2-for-sql-server-2017"
$ie.silent = $true
$ie.navigate($url)
while($ie.Busy) { Start-Sleep -Milliseconds 100 }
Start-Sleep 10
$ie.Document.documentElement.innerHTML > "C:\Tempfiles\output.html"
$ie.Stop()
$ie.Quit()

然后

     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;
       }
    }