使用存储过程“当前命令发生了严重错误。应放弃任何可能产生的结果”
代码
user.cs
#region//用户发布信息
public int InsertInfor(string Title,string contents,string phone,int type,string inputTime,int School,int SmClass)
{
SqlParameter[] prams = {
IntParam( "@Title ", SqlDbType.VarChar, 50, Title),
IntParam( "@contents ", SqlDbType.Text, 16, contents),
IntParam( "@phone ", SqlDbType.VarChar, 50, phone),
IntParam( "@type ", SqlDbType.Int, 4, type),
IntParam( "@inputTime ", SqlDbType.VarChar, 50, inputTime),
IntParam( "@School ", SqlDbType.Int, 4, School),
IntParam( "@SmClass ", SqlDbType.Int, 50, SmClass),
OutParam( "@InforsID ",SqlDbType.Int,4)
};
RunInsert( "InsertInfor ", prams);
return Convert.ToInt32(prams[7].Value);
//return Convert.ToInt32(prams[0].Value);
}
#endregion
SqlDataBase.cs
#region 创建一个Command来执行存储过程
private SqlCommand ReturnCM(string RunNM, SqlParameter[] PartNM)
{
Open();
SqlCommand ThisCm = new SqlCommand(RunNM,Cn);
ThisCm.CommandType = CommandType.StoredProcedure;
//传入参数
if( PartNM != null)
{
foreach (SqlParameter parmt in PartNM)
ThisCm.Parameters.Add(parmt);
}
//加入返回参数
ThisCm.Parameters.Add(new SqlParameter( "ReturnValue ", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return ThisCm;
}
#endregion
#region 返回带参数的存储过程
public int RunInsert(string RunName, SqlParameter[] PartName)
{
SqlCommand MyCM = ReturnCM(RunName, PartName);
MyCM.ExecuteNonQuery();
this.Close();
return (int)MyCM.Parameters[ "ReturnValue "].Value;
}
#endregion
#region //传入输入的参数
public SqlParameter IntParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output,Value);
}
#endregion
#region //传入返回值参数
public SqlParameter OutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
#endregion
#region //生成存储过程参数
private SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
param = new SqlParameter(ParamName, DbType);
}
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value;
return param;
}
#endregion
大家帮忙看看 为什么执行到MyCM.ExecuteNonQuery();报错误啊??
存储过程:
CREATE Procedure InsertInfor
(
@Title varchar(50),
@contents text,
@phone varchar(50),
@type int,
@inputTime datetime,
@School int,
@SmClass int,
@InforID int output
)
AS
Begin
Insert Into Infor (Title,contents,phone,type,inputTime,School,SmClass) values(@Title,@contents,@phone,@type,@inputTime,@School,@SmClass);
Select @InforID = @@IDENTITY
End
GO
大家帮帮忙啊~~~偶第一次在程序里用存储过程啊~~~~~
[解决办法]
不好意思,看到头昏都没看出来
你的代码太长
只有帮你顶了