本文實例講述了asp.net中調(diào)用存儲過程的方法。分享給大家供大家參考,具體如下:
一、建立并調(diào)用一個不帶參數(shù)的存儲過程如下:
CREATE PROCEDURE 全部學(xué)生dbo.selectUsers> AS SELECT * FROM 學(xué)生 GO EXEC 全部學(xué)生
建立并調(diào)用一個帶參數(shù)的存儲過程如下:
CREATE PROCEDURE 學(xué)生查詢1 @SNAME VARCHAR(8),@SDEPT VARCHAR(20) AS SELECT * FROM 學(xué)生 WHERE 姓名=@SNAME AND 所在系=@SDEPT GO EXEC 學(xué)生查詢1 '張三','計算機系'
或:
EXEC 學(xué)生查詢1 @SNAME='張三',@SDEPT='計算機系'
(2)刪除存儲過程:
DROP PROCEDURE存儲過程名組>
二、在asp.net中調(diào)用存取過程:
DBHelper.cs
//不帶參數(shù) 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]; } //帶參數(shù) 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("@參數(shù)1", 值1); //cmd.Parameters.AddWithValue("@參數(shù)2", 值2); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; }
UsersService.cs
//不帶參數(shù) public static IListUsers> GetUserList() { ListUsers> list = new ListUsers>(); 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; } //帶參數(shù) public static IListUsers> GetUserList(string userName,string password) { ListUsers> list = new ListUsers>(); 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; }
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對大家asp.net程序設(shè)計有所幫助。
標(biāo)簽:本溪 吉林 麗江 河南 宜春 汕頭 重慶 婁底
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net中調(diào)用存儲過程的方法》,本文關(guān)鍵詞 asp.net,中,調(diào)用,存儲,過程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。