我正在开发一个我的SQL查询
的项目SELECT Name
FROM techer_reg
LIMIT 3
如何添加整数值而不是3?
从某种意义上说,这个选择查询应该如下所示:
SELECT Name
FROM techer_reg
LIMIT (My integer value)
答案 0 :(得分: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);
}
}
}
}
答案 1 :(得分:0)
过程:
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)