asp.net中調用存儲過程的方法
本文實例講述了asp.net中調用存儲過程的方法。分享給大家供大家參考,具體如下:
一、建立并調用一個不帶參數的存儲過程如下:
CREATE PROCEDURE 全部學生<dbo.selectUsers> AS SELECT * FROM 學生 GO EXEC 全部學生
建立并調用一個帶參數的存儲過程如下:
CREATE PROCEDURE 學生查詢1 @SNAME VARCHAR(8),@SDEPT VARCHAR(20) AS SELECT * FROM 學生 WHERE 姓名=@SNAME AND 所在系=@SDEPT GO EXEC 學生查詢1 '張三','計算機系'
或:
EXEC 學生查詢1 @SNAME='張三',@SDEPT='計算機系'
(2)刪除存儲過程:
DROP PROCEDURE<存儲過程名組>
二、在asp.net中調用存取過程:
DBHelper.cs
//不帶參數
public static DataTable GetList(string sqlDBO)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sqlDBO, Connection);
cmd.CommandType = CommandType.StoredProcedure; //指定命令類型為存儲過程
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
//帶參數
public static DataTable GetList(string sqlDBO,params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sqlDBO, Connection);
cmd.CommandType = CommandType.StoredProcedure; //指定命令類型為存儲過程
cmd.Parameters.AddRange(values);
//cmd.Parameters.AddWithValue("@參數1", 值1);
//cmd.Parameters.AddWithValue("@參數2", 值2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
UsersService.cs
//不帶參數
public static IList<Users> GetUserList()
{
List<Users> list = new List<Users>();
DataTable table = DBHelper.GetList("存儲過程名稱");
foreach (DataRow row in table.Rows)
{
Users users = new Users();
users.Id=(int)row["id"];
users.UserName=(string)row["userName"];
users.Password=(string)row["password"];
list.Add(users);
}
return list;
}
//帶參數
public static IList<Users> GetUserList(string userName,string password)
{
List<Users> list = new List<Users>();
SqlParameter[] para=new SqlParameter[]
{
new SqlParameter("@userName",userName),
new SqlParameter("@password",password)
};
DataTable table = DBHelper.GetList("存儲過程名稱",para);
foreach (DataRow row in table.Rows)
{
Users users = new Users();
users.Id=(int)row["id"];
users.UserName=(string)row["userName"];
users.Password=(string)row["password"];
list.Add(users);
}
return list;
}
更多關于asp.net相關內容感興趣的讀者可查看本站專題:《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結專題》及《asp.net緩存操作技巧總結》。
希望本文所述對大家asp.net程序設計有所幫助。
相關文章
將選擇的圖片顯示在listview中,并顯示filename,path和type的簡單實例
這篇文章介紹了將選擇的圖片顯示在listview中,并顯示filename,path和type的簡單實例,有需要的朋友可以參考一下2013-10-10
基于.net standard 的動態(tài)編譯實現代碼
這篇文章主要介紹了基于.net standard 的動態(tài)編譯實現代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
asp.net 使用Response.Filter 過濾非法詞匯
一般信息發(fā)布網站,論壇等均具有實現非法詞匯過濾功能,即當用戶錄入非法詞匯時,進行替換,使其無法顯示到頁面上,針對此種功能,通常采用的時,在讀取時,在讀到非法詞匯后,進行替換。2010-03-03
詳解Asp.Net Core 發(fā)布和部署( MacOS + Linux + Nginx )
這篇文章主要介紹了詳解Asp.Net Core 發(fā)布和部署( MacOS + Linux + Nginx ) ,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
asp.net core標簽助手的高級用法TagHelper+Form
這篇文章主要為大家詳細介紹了asp.net core標簽助手的高級用法TagHelper+Form,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

