日期:2014-05-18  浏览次数:20759 次

C#调用有返回参数的sql存储过程
我在编写一个用c#和sql server 2000的小系统,但是调用有返回值的存储过程老出错
 chaxun表定义
Gno int identity(1,1) primary key
Gname char(20) ,
shijian datetime


存储过程如下:
ALTER PROCEDURE xianshi
@shijian datetime,
@Gno int output
as
select Gno from chuxun
where shijian=@shijian
在c#程序调用此过程如下函数
  public string load2(DateTime @shijian)
  {
  DateTime shijian1 = shijian;
  SqlConnection thisConnection = new SqlConnection("server=zhengfen;Integrated Security=True;" + "Database=gongcheng1;");
  SqlCommand thisCommand = new SqlCommand("xianshi", thisConnection);
  thisCommand.CommandType = CommandType.StoredProcedure;

  SqlParameter parametershijian = new SqlParameter("@shijian", SqlDbType.DateTime);
  parametershijian.Direction = ParameterDirection.Input;
  parametershijian.Value = @shijian;
  thisCommand.Parameters.Add(parametershijian);

  SqlParameter parameterGno = new SqlParameter("@Gno", SqlDbType.Int, 4);
  parameterGno.Direction = ParameterDirection.ReturnValue ;
  thisCommand.Parameters.Add(parameterGno);
  try
  {
  thisConnection.Open();
  thisCommand.ExecuteNonQuery();
   
  char Gno = (char)parameterGno.Value;
  thisConnection.Close();
  return Gno.ToString ();
  }

  catch
  {
  return String.Empty;
  }

  }

希望返回值给textBox.text=load2(shijian)
其中shijian=System.DateTime.Now

小妹刚学习c#,希望各位大侠多帮帮忙

------解决方案--------------------
我帮你改了个,自己调通了,你试试

存储过程:
create proc xianshi 
@shijian datetime, 
@Gno int output 
as 
select @Gno = Gno from chuxun 
where shijian=@shijian 
return @Gno

C#代码
public string load2(DateTime shijian)
{
DateTime shijian1 = shijian;
SqlConnection thisConnection = new SqlConnection("Data Source=.;Initial Catalog=AS;User ID=sa");
SqlCommand thisCommand = new SqlCommand("xianshi", thisConnection);
thisCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parametershijian = new SqlParameter("@shijian", SqlDbType.DateTime);
parametershijian.Direction = ParameterDirection.Input;
parametershijian.Value = shijian;
thisCommand.Parameters.Add(parametershijian);

SqlParameter parameterGno = new SqlParameter("@Gno", SqlDbType.Int, 4);
parameterGno.Direction = ParameterDirection.Output;
thisCommand.Parameters.Add(parameterGno);

try
{
thisConnection.Open();
thisCommand.ExecuteNonQuery();

string s = parameterGno.Value.ToString();
thisConnection.Close();
return s;
}
catch
{
return String.Empty;
}