在C#中从另一个调用一个存储过程

时间:2010-08-08 11:02:26

标签: c# sql-server c#-3.0

我有错误'将数据类型nvarchar转换为十进制时出错' 当调用存储过程从C#调用另一个存储过程

cmd = new SqlCommand("tax_Base_emp", con);
cmd.CommandType = CommandType.StoredProcedure;
parm1 = new SqlParameter("@emp_code", SqlDbType.BigInt); 
parm1.Value = emp_code; 
parm1.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm1);
parm2 = new SqlParameter("@co_id", SqlDbType.BigInt); 
parm2.Value = Settings.Default.comp_id; 
parm2.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm2);
parm3 = new SqlParameter("@d", SqlDbType.DateTime); 
parm3.Value = Convert.ToDateTime("31/1/2010"); 
parm3.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm3);
parm4 = new SqlParameter("@y", SqlDbType.Int); 
parm4.Value =int.Parse(textBox2.Text); 
parm4.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm4);
parm5 = new SqlParameter("@check_month", SqlDbType.Int); 
parm5.Value =1; 
parm5.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm5);
parm6 = new SqlParameter("@month", SqlDbType.Int); 
parm6.Value =8; 
parm6.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm6);
SqlParameter parm7 = new SqlParameter("@indate", SqlDbType.DateTime); 
parm7.Value = Convert.ToDateTime("8/5/2010"); 
parm7.Direction = ParameterDirection.Input; 
cmd.Parameters.Add(parm7);
SqlParameter parm8 = new SqlParameter("@Sumtotal", SqlDbType.Decimal); 
parm8.Scale = 2; 
parm8.Direction = ParameterDirection.Output; 
cmd.Parameters.Add(parm8);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
decimal tax_value = Convert.ToDecimal(cmd.Parameters["@Sumtotal"].Value);

存储的proecdure称为:

ALTER  PROCEDURE  Tax_Base_emp
  @emp_code bigint,
  @co_id bigint,
  @d datetime,
  @y int,
  @check_month int,
  @month int,
  @indate datetime,
  @Sumtotal decimal(8,2) output

AS
 declare  @tax_main_sal decimal(8,2)  
 declare  @tax_var_sal decimal(8,2)
 declare @salary decimal(8,2)
 declare @insh_varsalary decimal(8,2)
 declare @insh_value decimal(8,2)
 declare @vacation_value decimal(8,2)
 declare @vacation_varsalary decimal(8,2)
 declare @ded_value decimal(8,2) 
 declare @ben_value decimal(8,2) 


exec Taxable_mainsalary @emp_code,@co_id,@tax_main_sal output
exec taxable_varsalary @emp_code,@co_id, @tax_var_sal output
----taxableSalary---------------
set @salary=@tax_main_sal+@tax_var_sal
----insurance-------------------
exec  varsalary_insh @emp_code,@co_id,@d,@y, @insh_varsalary  output
exec insh_no @emp_code,@co_id,@insh_varsalary,@check_month, @insh_value output
----vacation--------------------
exec  vacation_varsalary @emp_code,@co_id,@vacation_varsalary output
exec  vacation_value @emp_code,@co_id,@y,@month,@vacation_varsalary,output
---------deduction--------------- 
exec deduction_for_tax @emp_code,@co_id,@indate,@ded_value output
-------------benifit------------
exec benfit_for_tax @emp_code,@co_id,@indate,@ben_value output
-----------------------------------NetSalary--------------------------------------------------------
 set @Sumtotal=(isnull(@salary,0)+isnull(@ben_value,0))-(isnull(@insh_value,0)+isnull(@vacation_value,0)+isnull(@ded_value,0))
return 

2 个答案:

答案 0 :(得分:3)

我认为您的C#代码没有任何问题 - 很难说出导致问题的原因。你的C#代码只是简单地调用一个存储过程 - 这应该不是问题,真的。

但是,我确实对您的编码风格提出了一些建议:

  • 将您的SqlConnectionSqlCommand放入using(....) { .... }块,以使您的代码更可靠
  • 尽量避免一遍又一遍地指定默认属性值,例如Direction = ParameterDirection.Input;; .Input是默认值 - 仅在您偏离默认值时指定它
  • 如果你一遍又一遍地执行相同的步骤 - 为什么不把它放在一个方法中并调用该方法几次?这也使您不必创建大量的SqlParameter对象,然后扔掉......

你最终会得到类似的东西:

public void CallStoredProc()
{
    using(SqlConnection con = new SqlConnection(.....))
    using(SqlCommand cmd = new SqlCommand("tax_Base_emp", con))
    {
       cmd.CommandType = CommandType.StoredProcedure;

       AddParameter(cmd.Parameters, "@emp_code", SqlDbType.BigInt, emp_code);
       AddParameter(cmd.Parameters, "@co_id", SqlDbType.BigInt, comp_id);
       AddParameter(cmd.Parameters, "@d", SqlDbType.DateTime, Convert.ToDateTime("31/1/2010"));
       AddParameter(cmd.Parameters, "@y", SqlDbType.Int, int.Parse(textBox2.Text));
       AddParameter(cmd.Parameters, "@check_month", SqlDbType.Int, 1);
       AddParameter(cmd.Parameters, "@month", SqlDbType.Int, 8);
       AddParameter(cmd.Parameters, "@indate", SqlDbType.DateTime, Convert.ToDateTime("8/5/2010"));

       AddOutputParameter(cmd.Parameters, "@Sumtotal", SqlDbType.Decimal, 8, 2);

       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();

       decimal tax_value = Convert.ToDecimal(cmd.Parameters["@Sumtotal"].Value);
   }
}

public void AddParameter(SqlParameterCollection params, string name, SqlDbType type, object value)
{
    SqlParameter tmpParam = new SqlParameter(name, type); 
    tmpParam.Value = value; 
    params.Add(tmpParam);
}

public void AddOutputParameter(SqlParameterCollection params, string name, SqlDbType type, int precision, int scale)
{
    SqlParameter tmpParam = new SqlParameter(name, type); 
    tmpParam.ParameterDirection = Direction.Output;
    tmpParam.Precision = precision; 
    tmpParam.Scale = scale; 
    params.Add(tmpParam);
}

答案 1 :(得分:0)

我在你发布的代码中看不到会导致这种情况的任何内容。

我怀疑错误发生在您调用的8个存储过程之一中,您将nvarchar分配给小数。

我暂时将这些评论全部评论出来并返回一个虚拟数字,如果修复它取消注释,再试一次。如果错误重新出现,那么您知道它就是您刚刚取消注释的错误之一。即进行二元搜索以找到违规程序。