华域联盟 .Net asp.net中调用存储过程的方法

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程序设计有所帮助。

您可能感兴趣的文章:

  • asp.net安全、实用、简单的大容量存储过程分页
  • asp.net 结合mysql存储过程进行分页代码
  • asp.net 存储过程调用
  • 在ASP.NET中用存储过程执行SQL语句
  • asp.net sql存储过程
  • asp.net利用存储过程和div+css实现分页(类似于博客园首页分页)
  • asp.net结合aspnetpager使用SQL2005的存储过程分页
  • ASP.NET 获取存储过程返回值的实现代码
  • asp.net安全、实用、简单的大容量存储过程分页
  • asp.net中IDataParameter调用存储过程的实现方法

本文由 华域联盟 原创撰写:华域联盟 » asp.net中调用存储过程的方法

转载请保留出处和原文链接:https://www.cnhackhy.com/64724.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部