[英]How do I store a SQL stored procedure result that returns a row or a column value into an ASP.NET C# variable?
[英]Get the value of particular column via stored procedure in ASP.NET C#
我的存儲過程有一個 select 語句,即: select * from studenttable where studentid=@studentid
學生表還有列studentid, name, city
當我在文本框中輸入學生 ID 時,它應該獲取姓名和城市並分別顯示在 LabelName 和 LabelCity 中。
我做了什么:
bool attempt = dl.OpenSQLConnection();
SqlCommand cmd = new SqlCommand("VerifyStudent");
cmd.Connection = dl.myConn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@STUDENTID", studentid)
cmd.ExecuteNonQuery();
return true;
我希望,您想檢查用戶名“ABC”或任何用戶名是否存在。 如果需要的用戶名和密碼匹配,此 sp 將返回大於零計數的值。 傳遞 'ABC' 或任何用戶名作為用戶名參數。
create procedure ValidateAdminLogin
(@UserName varchar(50),@Pasword varchar(50))
as
begin
select count(*) from user_creation where user_name=@UserName and password=@Pasword
end
我建議您編寫存儲過程並編寫這樣的查詢並從 c# 代碼中調用它。 將 user_name 傳遞給存儲過程。
select count (user_name) from user_creation nolock where user_name= @username
注意:避免在查詢中使用select *
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.