源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

asp.net中调用存储过程的方法

  • 时间:2022-12-03 14:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:asp.net中调用存储过程的方法
本文实例讲述了asp.net中调用存储过程的方法。分享给大家供大家参考,具体如下: [b]一、建立并调用一个不带参数的存储过程如下:[/b]
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<存储过程名组>

[b]二、在asp.net中调用存取过程:[/b] 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相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/692.htm]asp.net字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/660.htm]asp.net操作XML技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/306.htm]asp.net文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/535.htm]asp.net ajax技巧总结专题[/url]》及《[url=http://www.1sucai.cn/Special/626.htm]asp.net缓存操作技巧总结[/url]》。 希望本文所述对大家asp.net程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部