C#中调用有输入参数,有输出的存储过程;具体情况如下代码
数据库中存储过程的创建:createproceduresuminout@inouttypechar(4),@usernamechar(20),@starttimedate...
数据库中存储过程的创建:
create procedure suminout
@inouttype char(4),
@username char(20),
@starttime date,
@endtime date,
@summoney float output
as
select @summoney =SUM(cast(amount as float)) from inout
where inouttype =@inouttype and name=@username and dates >=@starttime and dates <=@endtime
项目中创建的一个连接类(构造函数中已设好了连接字符等)中的一个针对于调用此存储过程的一个方法为:
public float proceduregetData(string procedureName,string inouttype,string username,string starttime,string endtime)
{
float total;
SqlCommand command = new SqlCommand(procedureName, conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.Parameters.Add("@inouttype", SqlDbType.VarChar,4).Value = inouttype;
command.Parameters["@inouttype"].Direction = ParameterDirection.Input;
command.Parameters.Add("@username", SqlDbType.VarChar ,20).Value = username;
command.Parameters["@username"].Direction = ParameterDirection.Input;
command.Parameters.Add("@starttime" , SqlDbType.Date ).Value = Convert .ToDateTime (starttime );
command.Parameters["@starttime"].Direction = ParameterDirection.Input;
command.Parameters.Add("@endtime", SqlDbType.Date).Value = Convert.ToDateTime(endtime);
command.Parameters["@endtime"].Direction = ParameterDirection.Input;
command.Parameters.Add("@summoney", SqlDbType.Float );
command.Parameters["@summoney"].Direction = ParameterDirection.Output;
command.ExecuteScalar();
total = (float)command.Parameters["@summoney"].Value;//调试时此语句要报错,无法转换此类型。
command.Connection.Close();
return total;
}
在窗体中调用的代码为:
sql one = new sql();//之前说的创建的连接类
float sumin, sumout;
sumin = 0;
sumout = 0;
sumin = one.proceduregetData("suminout","收入", username.Text,starttime .Text , endtime.Text);
sumout = one.proceduregetData("suminout", "支出", username.Text, starttime.Text, endtime.Text);
allin.Text = sumin.ToString();
大概就是这样,但调试时, 展开
create procedure suminout
@inouttype char(4),
@username char(20),
@starttime date,
@endtime date,
@summoney float output
as
select @summoney =SUM(cast(amount as float)) from inout
where inouttype =@inouttype and name=@username and dates >=@starttime and dates <=@endtime
项目中创建的一个连接类(构造函数中已设好了连接字符等)中的一个针对于调用此存储过程的一个方法为:
public float proceduregetData(string procedureName,string inouttype,string username,string starttime,string endtime)
{
float total;
SqlCommand command = new SqlCommand(procedureName, conn);
command.CommandType = CommandType.StoredProcedure;
conn.Open();
command.Parameters.Add("@inouttype", SqlDbType.VarChar,4).Value = inouttype;
command.Parameters["@inouttype"].Direction = ParameterDirection.Input;
command.Parameters.Add("@username", SqlDbType.VarChar ,20).Value = username;
command.Parameters["@username"].Direction = ParameterDirection.Input;
command.Parameters.Add("@starttime" , SqlDbType.Date ).Value = Convert .ToDateTime (starttime );
command.Parameters["@starttime"].Direction = ParameterDirection.Input;
command.Parameters.Add("@endtime", SqlDbType.Date).Value = Convert.ToDateTime(endtime);
command.Parameters["@endtime"].Direction = ParameterDirection.Input;
command.Parameters.Add("@summoney", SqlDbType.Float );
command.Parameters["@summoney"].Direction = ParameterDirection.Output;
command.ExecuteScalar();
total = (float)command.Parameters["@summoney"].Value;//调试时此语句要报错,无法转换此类型。
command.Connection.Close();
return total;
}
在窗体中调用的代码为:
sql one = new sql();//之前说的创建的连接类
float sumin, sumout;
sumin = 0;
sumout = 0;
sumin = one.proceduregetData("suminout","收入", username.Text,starttime .Text , endtime.Text);
sumout = one.proceduregetData("suminout", "支出", username.Text, starttime.Text, endtime.Text);
allin.Text = sumin.ToString();
大概就是这样,但调试时, 展开
1个回答
展开全部
(1)执行一个没有参数的存储过程的代码如下:
SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.selectCommand = new SqlCommand();
da.selectCommand.Connection = conn;
da.selectCommand.CommandText = "NameOfProcedure";
da.selectCommand.CommandType = CommandType.StoredProcedure;
(2)执行一个有参数的存储过程的代码如下
SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.selectCommand = new SqlCommand();
da.selectCommand.Connection = conn;
da.selectCommand.CommandText = "NameOfProcedure";
da.selectCommand.CommandType = CommandType.StoredProcedure;
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.Input;
param.Value = Convert.ToDateTime(inputdate);
da.selectCommand.Parameters.Add(param);
若需要添加输出参数:
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.Output;
param.Value = Convert.ToDateTime(inputdate);
da.selectCommand.Parameters.Add(param);
若要获得参储过程的返回值:
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.ReturnValue;
param.Value = Convert.ToDateTime(inputdate);
da.selectCommand.Parameters.Add(param);
SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.selectCommand = new SqlCommand();
da.selectCommand.Connection = conn;
da.selectCommand.CommandText = "NameOfProcedure";
da.selectCommand.CommandType = CommandType.StoredProcedure;
(2)执行一个有参数的存储过程的代码如下
SqlConnection conn=new SqlConnection(“connectionString”);
SqlDataAdapter da = new SqlDataAdapter();
da.selectCommand = new SqlCommand();
da.selectCommand.Connection = conn;
da.selectCommand.CommandText = "NameOfProcedure";
da.selectCommand.CommandType = CommandType.StoredProcedure;
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.Input;
param.Value = Convert.ToDateTime(inputdate);
da.selectCommand.Parameters.Add(param);
若需要添加输出参数:
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.Output;
param.Value = Convert.ToDateTime(inputdate);
da.selectCommand.Parameters.Add(param);
若要获得参储过程的返回值:
param = new SqlParameter("@ParameterName", SqlDbType.DateTime);
param.Direction = ParameterDirection.ReturnValue;
param.Value = Convert.ToDateTime(inputdate);
da.selectCommand.Parameters.Add(param);
追问
public float proceduregetData(……)方法体如上,这个方法是执行存储过程的,第一个参数传递的是存储过程名称,方法里,如上面我定义了total,float类型的,我想要来接收存储过程的返回值,再作为这个方法的返回值,
total = (float)command.Parameters["@summoney"].Value;//调试时此语句要报错,指定类型转换无效。怎么接收存储过程的反回值哇……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询