调用存储过程 [英] Call a Stored Procedure

查看:108
本文介绍了调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何提示在尝试调用存储的项目时可能出现错误的地方.它返回null并应包含ID.

 SqlConnection con =  SqlConnection( @" );

con.Open();

SqlCommand cmdselect =  SqlCommand(" ,con);
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.CommandText = " ;

ddlistschools.SelectedValue);
cmdselect.Parameters.Add( SqlParameter(" ,ddlistschools.SelectedValue));
              
cmdselect.Parameters [" ].Direction = ParameterDirection.Output;

 int 结果;
结果=( int )cmdselect.ExecuteScalar(); 



结果应该包含一个ID

解决方案

除非看到存储过程的内容,否则我将无法提供具体的帮助,但是这里有些想法让我无所适从:
  • 也许ExecuteScalar返回SqlInt32而不是int.
  • 也许您应该获得输出参数的值.
  • 也许@College传入了数据类型错误(例如,可能是int类型,但可能正在传递字符串).
  • 也许您做错了什么,并且没有基于存储类型从存储过程返回的记录您输入的内容.

您可以通过一些方法解决此问题:
  • 将对存储过程的所有调用(例如,对另一个表的调用)记录到存储过程中,以便您可以从SQL Server Management Studio自己运行它们.
  • 使用SQL事件探查器准确查看正在传递的内容
  • 设置一些断点以确保所有值都符合您的期望(例如ddlistschools.SelectedValue).


Any Hints where the error could be while trying to call a stored project.It returns a null whereby it should contain a ID.

SqlConnection con = new SqlConnection(@"Data Source=FNUEXCPC01\SQLSERVER2005;Initial Catalog=Record_Management;Integrated Security=True");

con.Open(); 

SqlCommand cmdselect = new SqlCommand("RecordManagement_getcollegeid", con);
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.CommandText = "RecordManagement_getcollegeid";

ddlistschools.SelectedValue);
cmdselect.Parameters.Add(new SqlParameter("@College",ddlistschools.SelectedValue));
              
cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;

int Results;          
Results = (int)cmdselect.ExecuteScalar();



Results should contain a ID

解决方案

I can''t give specific help unless I see the stored procedure contents, but here are some ideas off the top of my head:
  • Maybe ExecuteScalar is returning SqlInt32 rather than int.
  • Maybe you are supposed to be getting the value of the output parameter.
  • Maybe @College is being passed in as the wrong data type (e.g., maybe an int is expected, but maybe you are passing in a string).
  • Maybe you did something wrong and there are no records to return from the stored procedure based on the inputs you gave it.

Some ways you can solve this:
  • Log any calls (e.g., to a different table) to the stored procedure so you can run them yourself from SQL Server Management Studio.
  • Use SQL Profiler to see exactly what is being passed to SQL Server.
  • Set some breakpoints to be sure all your values are as you expect (e.g., ddlistschools.SelectedValue).


这篇关于调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆