SQL查询数据到cshtml

时间:2015-01-22 18:14:21

标签: c# sql asp.net razor ado.net

我不是很好但我正在尝试。我觉得有些东西我不明白...... 我试图从数据库获得统计数据,例如有多少行得到了" X"。看起来很简单我知道它的SQL语句。周围有很多演练。但我不知道如何让它出现在页面上。

if(!Request.QueryString["RNum"].IsEmpty() ) {

   searchTerm = Request.QueryString["RNum"];
 selectCommand2 = "SELECT COUNT(NoEmpl) FROM DTool Where NoEmpl = @0";
}
var Count = db.QueryValue(selectCommand2, searchTerm);

使用提交按钮发送查询如何让它显示在页面上?

1 个答案:

答案 0 :(得分:0)

试试这个

searchTerm = Request.QueryString["RNum"];
string sqlSelect = "SELECT COUNT(NoEmpl) FROM DTool Where NoEmpl= @NoEmpl";

SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);

// Set SqlDbType based on your DB column Data-Type 
sqlCommand.Parameters.Add("@NoEmpl", System.Data.SqlDbType.Varcahr);


 sqlCommand.Parameters["@NoEmpl"].Value = searchTerm ;

OR

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();      
        using (SqlCommand command = new SqlCommand(
        "SELECT COUNT(NoEmpl) FROM DTool Where NoEmpl= @NoEmpl", connection))
        {
        //
        // Add new SqlParameter to the command.
        //
        command.Parameters.Add(new SqlParameter("@NoEmpl", searchTerm ));
        //
        // Read in the SELECT results.
        //
        SqlDataReader reader = command.ExecuteReader();
        //read here
        }
    }