0

我正在做一个我的 sql 查询所在的项目

SELECT Name 
FROM techer_reg 
LIMIT 3

如何添加一个整数值而不是 3?

从某种意义上说,这个选择查询应该是这样的:

SELECT Name 
FROM techer_reg 
LIMIT (My integer value)
4

2 回答 2

4

尝试使用参数:

using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
int myLimit = 4;
using (SqlCommand com = new SqlCommand("SELECT Name FROM techer_reg LIMIT @LM", con))
    {
    com.Parameters.AddWithValue("@LM", myLimit);
    using (SqlDataReader reader = com.ExecuteReader())
        {
        while (reader.Read())
            {
            int id = (int)reader["iD"];
            string desc = (string)reader["description"];
            Console.WriteLine("ID: {0}\n    {1}", iD, desc);
            }
        }
    }
}
于 2012-04-17T07:55:42.657 回答
1

程序:

DELIMITER $

create PROCEDURE getData(limit INT)
begin
   SET @limit= limit;
   PREPARE exec_statment FROM "SELECT Name FROM techer_reg LIMIT limit ?;";
   EXECUTE exec_statment USING @limit;
   DEALLOCATE PREPARE exec_statment;
end$

DELIMITER ;

并使用

call getData(3)
于 2012-04-17T08:11:45.527 回答