数据库取多条数据用什么保存

lovefromsun3 2010-11-24 02:33:54

for (int i = 0; i < str.Length; i++)
{
string sqlstr = "select * from [user] where id ="+str[i];
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr, sqlcon);
......


取出多条数据后保存到list<>里,请人补充下代码- -#
...全文
206 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdpengfei1018 2010-11-25
  • 打赏
  • 举报
回复
顶,功能的实现和怎么样实现同等重要![Quote=引用 6 楼 qq601924881 的回复:]
先给你补上代码

C# code

try
{
sqlcon .Open();
SqlDataReader myReader = sqlcom .ExecuteReader(CommandBehavior.CloseConnection);
return myRe……
[/Quote]
sglogin 2010-11-25
  • 打赏
  • 举报
回复
执行多条语句的中间应该有事务
javaANDcobol 2010-11-24
  • 打赏
  • 举报
回复



string sqlstr = "select * from [user] where 1 = 1";
for (int i = 0; i < str.Length; i++)
{
sqlstr += " And id =" + str[i];
}
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr, sqlcon);
查询一次数据库可以像上面那样做,多条数据用DataTable就行了,没有必要转换成List<T>的。




山书生 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhoufoxcn 的回复:]
多条数据用DataTable就行了,没有必要转换成List<T>的。
[/Quote]
正解!
coolbeelove 2010-11-24
  • 打赏
  • 举报
回复
哪里下一个后台模版呀?[Quote=引用楼主 lovefromsun3 的回复:]
C# code

for (int i = 0; i < str.Length; i++)
{
string sqlstr = "select * from [user] where id ="+str[i];
sqlcon = new SqlConnection(strCon);
sqlc……
[/Quote]
周公 2010-11-24
  • 打赏
  • 举报
回复
多条数据用DataTable就行了,没有必要转换成List<T>的。
chen_ya_ping 2010-11-24
  • 打赏
  • 举报
回复
list,
dataset
datatable都可以的。
qq601924881 2010-11-24
  • 打赏
  • 举报
回复
先给你补上代码

try
{
sqlcon .Open();
SqlDataReader myReader = sqlcom .ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
//finally
//{
// sqlcom .Dispose();
sqlcon .Close();


然后要说的是你循环访问数据库打开关闭、打开关闭,你不嫌类啊
这个样

string strWhere = strimg.Empty;
for (int i = 0; i < str.Length; i++)
{
strWhere +=str[i]+",";
}
if(strWhere.length>0)
{
strWhere=strWhere.Remove(strWhere.LastIndexOf(","));
string sqlstr = "select * from [user] where id ="+str[i];
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr, sqlcon);
try
{
sqlcon .Open();
SqlDataReader myReader = sqlcom .ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
//finally
//{
// sqlcom .Dispose();
sqlcon .Close();

}

h465716405 2010-11-24
  • 打赏
  • 举报
回复
楼上正解。。以前我也用List<>..
public List<OaRole> GetAllOaRoleInfoByDepid(int depid)
{
List<OaRole> rolelist = new List<OaRole>();
try
{
SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@EmployeesDepartmentId", SqlDbType.NVarChar, 20) };
cmdParams[0].Value = depid;
SqlDataReader reader = SqlHelper2.ExecuteReader(SqlHelper2.connStr, CommandType.StoredProcedure, "GetAllOaRoleByDepid", cmdParams);
while (reader.Read())
{
OaRole role = new OaRole();
role.OaRoleId = int.Parse(reader["OaRoleId"].ToString().Trim());
role.RoleName = reader["RoleName"].ToString().Trim();
role.EmployeesDepartmentId = int.Parse(reader["EmployeesDepartmentId"].ToString());
rolelist.Add(role);
}

return rolelist;
}
catch (SqlException se)
{
throw se;
}

}
gdk123 2010-11-24
  • 打赏
  • 举报
回复
datatable里也可以啊!
xshf12345 2010-11-24
  • 打赏
  • 举报
回复
为什么放在list里面,放在DataSet里面不是很好么,改完数据后就可以直接持久化到数据库中的
hao1hao2hao3 2010-11-24
  • 打赏
  • 举报
回复
这效率真的...

还是考虑查询一次数据库吧!别循环多次查了,太浪费资源。
lovefromsun3 2010-11-24
  • 打赏
  • 举报
回复
学习学习

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧