存储过程output String[1]: Size 属性具有无效大小值0

  c#代碼:

using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SqlGwRegister"].ConnectionString.ToString()))
           {
               sqlcon.Open();
               // SqlCommand cmd = new SqlCommand("Pro_Sof_CheckMaterial", sqlcon);
               SqlCommand cmd = new SqlCommand("Pro_Sof_CheckMaterial_test", sqlcon);
               cmd.CommandType = CommandType.StoredProcedure;
              // cmd.Parameters.Add( new SqlParameter("@Year", SqlDbType.NVarChar));
               cmd.Parameters.Add("@Year", SqlDbType.NVarChar);
               cmd.Parameters["@Year"].Value = year;
             //  cmd.Parameters.Add(new SqlParameter("@Batch", SqlDbType.NVarChar));
               cmd.Parameters.Add("@Batch", SqlDbType.NVarChar);
                cmd.Parameters["@Batch"].Value = batch;
               //cmd.Parameters.Add(new SqlParameter("@OutMaterial", SqlDbType.NVarChar));
               cmd.Parameters.Add("@OutMaterial", SqlDbType.NVarChar, 4000);
               cmd.Parameters["@OutMaterial"].Direction = ParameterDirection.Output;
               cmd.ExecuteNonQuery();
               msg = cmd.Parameters["@OutMaterial"].Value.ToString();
           }

执行存储过程出现这个错误:存储过程output String[1]: Size 属性具有无效大小值0  

 


解決要點:output方式从存储过程传出参数 必须使用三个参数版本的,及Parameters.Add("("@OutMaterial", SqlDbType.VarChar,4000);

 

 

 

 

posted on 2015-11-26 17:13  明俠遠飄  阅读(488)  评论(0编辑  收藏  举报

导航