在服务资源管理器里执行存储过程,正常!以下是执行后显示的信息。@RV=15。但在VS解决方案里面测试却报错,test()函数最底行异常——“未将对象引用设置到对象的实例”。查看后发现paramOut.Value参数值为null! ------------------------------------------------------------------------------------------- 正在运行 [dbo].[Sp_GetFilmInfo] ( @id = 2, @RV = 10 ). 没有行受影响。(返回 1 行)@RV = 15@RETURN_VALUE = 完成 [dbo].[Sp_GetFilmInfo] 运行。 ---------------------------------------------------------------------------------------------以下是C#编写的存储过程文件名:Sp_GetFilmInfo.cs--------------------------------------------------------------------------------------------- [Microsoft.SqlServer.Server.SqlProcedure] public static void Sp_GetFilmInfo(SqlInt32 id,out SqlInt32 RV) { // 在此处放置代码 SqlConnection conn = new SqlConnection("context connection=true"); SqlCommand cmd = new SqlCommand(); conn.Open(); cmd.Connection = conn; cmd.CommandText = "select * from Film where id=@id"; SqlParameter param = new SqlParameter("@id",id); cmd.Parameters.Add(param); SqlDataReader dt = cmd.ExecuteReader(); SqlContext.Pipe.Send(dt); RV = 15; //设置输出参数为15 cmd.Dispose(); conn.Close(); dt.Close();
} ----------------------------------------------------------------------------------------------以下是调试存储过程函数;功能:输出@RV 这个参数---------------------------------------------------------------------------------------------- public string test() { SqlCommand cmd = new SqlCommand(); SqlParameter param1 = new SqlParameter("@id", 2);//查找ID为2的记录,此记录存在 cmd.Parameters.Add(param1);
SqlParameter paramOut = new SqlParameter("@RV",SqlDbType.Int, 10); paramOut.Direction = ParameterDirection.Output; //设置输出参数 cmd.Parameters.Add(paramOut); SqlConnection conn = new SqlConnection(SQLHelper.Con); //连接数据库 conn.Open(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = StoredProcedureName.Sp_GetFilmInfo.ToString();//获取存储过程名称,StoredProcedureName.Sp_GetFilmInfo为Model里枚举的存储过程名 SqlDataReader dtr = cmd.ExecuteReader(); return paramOut.Value.ToString(); }
} ----------------------------------------------------------------------------------------------以下是调试存储过程函数;功能:输出@RV 这个参数---------------------------------------------------------------------------------------------- public string test() { SqlCommand cmd = new SqlCommand(); SqlParameter param1 = new SqlParameter("@id", 2);//查找ID为2的记录,此记录存在 cmd.Parameters.Add(param1);
SqlParameter paramOut = new SqlParameter("@RV",SqlDbType.Int, 10); paramOut.Direction = ParameterDirection.Output; //设置输出参数 cmd.Parameters.Add(paramOut); SqlConnection conn = new SqlConnection(SQLHelper.Con); //连接数据库 conn.Open(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = StoredProcedureName.Sp_GetFilmInfo.ToString();//获取存储过程名称,StoredProcedureName.Sp_GetFilmInfo为Model里枚举的存储过程名 SqlDataReader dtr = cmd.ExecuteReader(); return paramOut.Value.ToString(); }
解决方案 »
- 这是BUG吗
- 如何将VS 2003 项目转换成VS2005或者2008?
- 开发好的ASP.net项目的安装问题。
- 如何得到某个类的字段的字定义特性(attribute),类可以用gettype().GetCcustomAttributes(),字段呢?
- VS2005团队开发(Team System)中怎么建项目?
- 如何将word 显示在asp.net页面上让起操作修改和保存?
- 有没人帮我下?.net网页无法打开的问题
- 最后60分,求呼叫中心对录音文件的质检方案,小女先谢谢各位了
- 高分求助 用Cache控制单次登陆问题
- 怪!!!关于treeview的事件的响应问题。
- 正则表达
- asp中fileupload实现图片预览的问题,求指点
需要把返回值写在SqlDataReader.close();后面!
还真是的,学习了。If your Command contains output parameters or return values, they will not be available until the DataReader is closed.来自msdn:
http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.71).aspx