先使用 sqlDataReader.close() 才能显示正确结果,百思不得其解 ~~~

xinuaile 2003-10-10 10:58:34
如果this.Response.Write("new productid= "+comm.Parameters["@id"].Value);

dr.Close();

结果没有 productid
必须 得 dr.Close();
this.Response.Write("new productid= "+comm.Parameters["@id"].Value);
==========================



SqlConnection conn=new SqlConnection("server=localhost;database=northwind;uid=sa;pwd=");
SqlCommand comm= conn.CreateCommand();
conn.Open();
comm.CommandText="exec @id= addP @name ,@supplierId ,@CategoryId ";
comm.Parameters.Add("@id",SqlDbType.Int);
comm.Parameters["@id"].Direction= ParameterDirection.Output;
comm.Parameters.Add("@name","zhao");
comm.Parameters.Add("@supplierid",1);
comm.Parameters.Add("@categoryId",1);

// comm.ExecuteNonQuery();
// this.Response.Write("new productid= "+comm.Parameters["@id"].Value);

SqlDataReader dr= comm.ExecuteReader();

while(dr.Read())
{
this.Response.Write("productname is "+dr["ProductName"]+ "UnitPrice is "+ dr["UnitPrice"]);
}


this.Response.Write("new productid= "+comm.Parameters["@id"].Value);

dr.Close();
...全文
39 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
coollzh 2004-01-31
  • 打赏
  • 举报
回复
呵呵,他们产品显现的机制就是这样的,具体他们为什么这么实现,微软没有公开呀,谁知道呢
xinuaile 2004-01-30
  • 打赏
  • 举报
回复
您把 原文 贴出来 , 俺看了文章 , 没有提到为什么
coollzh 2004-01-29
  • 打赏
  • 举报
回复
Using a DataReader to Process the RETURN Value and OUTPUT Parameters:

This same query can be executed directly with the Command.ExecuteReader (or ExecuteNonQuery if you don't have a rowset to process), but there are several other steps you'll need to take to process the returned values. Remember, you'll have to complete processing for all rowsets before attempting to capture the Return value or OUTPUT parameters. The following code shows how to use the ExecuteReader and a loop to process the rowsets, and then capture the Return value and OUTPUT parameters. You'll find that OUTPUT parameters (even a lot of them) can be handled far faster than even a single row of data returned by a SELECT.

Displaying the values of the Return Value and OUTPUT parameters

With cmd.Parameters
cn.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
' Process rowset(s)
bolEOF = dr.Read
Do
Do While bolEOF = True
' Process rows
bolEOF = dr.Read()
Loop
Loop While dr.NextResult = True
cmd.Cancel()
dr.Close()
Debug.WriteLine("@ReturnValue:" & _
.Item("@ReturnValue").Value.ToString)
Debug.WriteLine("@Title:" & _
.Item("@Title").Value.ToString)
Debug.WriteLine("@Type:" & _
.Item("@Type").Value.ToString)
Debug.WriteLine("@Advance:" & _
.Item("@Advance").Value.ToString)
Debug.WriteLine("@Price:" & _
.Item("@Price").Value.ToString)

这应该是ado.net的机制
xinuaile 2004-01-29
  • 打赏
  • 举报
回复
看了文章 , 没有提到为什么 !
xinuaile 2004-01-18
  • 打赏
  • 举报
回复
看了文章 , 没有提到为什么 !
zsww 2003-10-12
  • 打赏
  • 举报
回复
学习!

-------努力学习 不断实践 虚心讨教--------
saucer 2003-10-12
  • 打赏
  • 举报
回复
that is by design, see

Retrieving the Gazoutas: Understanding SQL Server Return Codes and Output Parameters
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/gazoutas.asp
xinuaile 2003-10-12
  • 打赏
  • 举报
回复
如果 有 返回值,必须先关闭 ? 能否 详细解释 一下, or 相关文档
xinuaile 2003-10-11
  • 打赏
  • 举报
回复
saucer 2003-10-11
  • 打赏
  • 举报
回复
yes, if you have an Output or ReturnValue, you need to close the reader first

62,025

社区成员

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

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

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

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