C# 如何将两个存储过程合并为一个方法并返回一个结果?
在这段代码中,我试图从SQL Server获取所有数据,该方法用于以网格形式列出用户及其卷。我这里的问题是如何调用/合并两个存储过程并返回一个结果,我的方法正确吗?如果没有,请解释一下,并告诉我正确的方法,这样我就可以在将来学习如何做。谢谢C# 如何将两个存储过程合并为一个方法并返回一个结果?,c#,stored-procedures,C#,Stored Procedures,在这段代码中,我试图从SQL Server获取所有数据,该方法用于以网格形式列出用户及其卷。我这里的问题是如何调用/合并两个存储过程并返回一个结果,我的方法正确吗?如果没有,请解释一下,并告诉我正确的方法,这样我就可以在将来学习如何做。谢谢 public IEnumerable<ApplicationUser> GetAllUsers() { try { List<ApplicationUser> userList = new List&l
public IEnumerable<ApplicationUser> GetAllUsers()
{
try
{
List<ApplicationUser> userList = new List<ApplicationUser>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("sp_UsersReadAll", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlCommand cmd2 = new SqlCommand("sp_GetUserRolls", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ApplicationUser apUser = new ApplicationUser();
apUser.Id = Convert.ToString(reader["Id"]);
apUser.UserName = reader["Username"].ToString();
apUser.FirstName = reader["FirstName"].ToString();
apUser.LastName = reader["LastName"].ToString();
apUser.Email = reader["Email"].ToString();
userList.Add(apUser);
}
con.Close();
}
return userList;
}
catch (Exception)
{
throw;
}
}
public IEnumerable GetAllUsers()
{
尝试
{
List userList=新列表();
使用(SqlConnection con=newsqlconnection(connectionString))
{
SqlCommand cmd=新的SqlCommand(“sp_UsersReadAll”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
SqlCommand cmd2=新的SqlCommand(“sp_GetUserRolls”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
con.Open();
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
ApplicationUser apUser=新的ApplicationUser();
apUser.Id=Convert.ToString(读卡器[“Id”]);
apUser.UserName=reader[“UserName”].ToString();
apUser.FirstName=reader[“FirstName”].ToString();
apUser.LastName=reader[“LastName”].ToString();
apUser.Email=reader[“Email”].ToString();
userList.Add(apUser);
}
con.Close();
}
返回用户列表;
}
捕获(例外)
{
投掷;
}
}
执行此任务可能有多种方法。比如说-
如果需要更多帮助,请共享ApplicationUser类的过程和属性的输出。您可以一次调用两个存储过程,然后使用Data
SqlDataReader.NextResult()
读取另一个结果集:
public IEnumerable<ApplicationUser> GetAllUsers()
{
try
{
List<ApplicationUser> userList = new List<ApplicationUser>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(@"exec sp_UsersReadAll
exec sp_GetUserRolls", con);
cmd.CommandType = System.Data.CommandType.Text;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ApplicationUser apUser = new ApplicationUser();
apUser.Id = Convert.ToString(reader["Id"]);
apUser.UserName = reader["Username"].ToString();
apUser.FirstName = reader["FirstName"].ToString();
apUser.LastName = reader["LastName"].ToString();
apUser.Email = reader["Email"].ToString();
userList.Add(apUser);
}
reader.NextResult();
while (reader.Read())
{
ApplicationUserRoll apUserRoll = new ApplicationUserRoll();
apUserRoll.Id = Convert.ToString(reader["Id"]);
apUserRoll.UserId = reader["UserId"].ToString();
userList.Single(u => u.Id == apUserRoll.UserId).Rolls.Add(apUserRoll);
}
con.Close();
}
return userList;
}
catch (Exception)
{
throw;
}
}
public IEnumerable GetAllUsers()
{
尝试
{
List userList=新列表();
使用(SqlConnection con=newsqlconnection(connectionString))
{
SqlCommand cmd=new SqlCommand(@“exec sp_UsersReadAll
执委会秘书长;
cmd.CommandType=System.Data.CommandType.Text;
con.Open();
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
ApplicationUser apUser=新的ApplicationUser();
apUser.Id=Convert.ToString(读卡器[“Id”]);
apUser.UserName=reader[“UserName”].ToString();
apUser.FirstName=reader[“FirstName”].ToString();
apUser.LastName=reader[“LastName”].ToString();
apUser.Email=reader[“Email”].ToString();
userList.Add(apUser);
}
reader.NextResult();
while(reader.Read())
{
ApplicationUserRoll apUserRoll=新ApplicationUserRoll();
apUserRoll.Id=Convert.ToString(读卡器[“Id]”);
apUserRoll.UserId=reader[“UserId”].ToString();
userList.Single(u=>u.Id==apUserRoll.UserId).Rolls.Add(apUserRoll);
}
con.Close();
}
返回用户列表;
}
捕获(例外)
{
投掷;
}
}
希望能有所帮助。类似的功能应该可以使用(我考虑您需要唯一的id)
public IEnumerable GetAllUsers()
{
尝试
{
应用程序用户;
List userList=新列表();
使用(SqlConnection con=newsqlconnection(connectionString))
{
SqlCommand cmd=新的SqlCommand(“sp_UsersReadAll”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
SqlCommand cmd2=新的SqlCommand(“sp_GetUserRolls”,con);
cmd2.CommandType=System.Data.CommandType.StoredProcess;
con.Open();
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
apUser=newapplicationuser();
apUser.Id=Convert.ToString(读卡器[“Id”]);
apUser.UserName=reader[“UserName”].ToString();
apUser.FirstName=reader[“FirstName”].ToString();
apUser.LastName=reader[“LastName”].ToString();
apUser.Email=reader[“Email”].ToString();
userList.Add(apUser);
}
//您可以用另一种方法重构此代码,而不必同时编写这两种方法
reader=cmd2.ExecuteReader();
while(reader.Read())
{
apUser=newapplicationuser();
apUser.Id=Convert.ToString(读卡器[“Id”]);
//如果id不存在,我们可以继续并添加该项
如果(!userList.Exists(o=>o.Id==apUser.Id))
{
apUser.UserName=reader[“UserName”].ToString();
apUser.FirstName=reader[“FirstName”].ToString();
apUser.LastName=reader[“LastName”].ToString();
apUser.Email=reader[“Email”].ToString();
userList.Add(apUser);
}
}
con.Close();
}
返回用户列表;
}
捕获(例外)
{
//事实上,这就像你没有尝试/抓住
投掷;
}
}
我只是告诉你方法,我没有优化代码。我想你必须循环twic
public IEnumerable<ApplicationUser> GetAllUsers()
{
try
{
ApplicationUser apUser;
List<ApplicationUser> userList = new List<ApplicationUser>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("sp_UsersReadAll", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlCommand cmd2 = new SqlCommand("sp_GetUserRolls", con);
cmd2.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
apUser = new ApplicationUser();
apUser.Id = Convert.ToString(reader["Id"]);
apUser.UserName = reader["Username"].ToString();
apUser.FirstName = reader["FirstName"].ToString();
apUser.LastName = reader["LastName"].ToString();
apUser.Email = reader["Email"].ToString();
userList.Add(apUser);
}
// You can refactor this code in another method for don't have to write it both
reader = cmd2.ExecuteReader();
while (reader.Read())
{
apUser = new ApplicationUser();
apUser.Id = Convert.ToString(reader["Id"]);
// If id doesn't exists, we can continue and add the item
if(!userList.Exists(o=>o.Id==apUser.Id))
{
apUser.UserName = reader["Username"].ToString();
apUser.FirstName = reader["FirstName"].ToString();
apUser.LastName = reader["LastName"].ToString();
apUser.Email = reader["Email"].ToString();
userList.Add(apUser);
}
}
con.Close();
}
return userList;
}
catch (Exception)
{
// Actually it's like if you had not try / catch
throw;
}
}