public DataTable VoteItemsShow(string ID,out string count)
{
SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@ID",ID),
new SqlParameter("@count",ParameterDirection.Output) //这个@count是存储过程中的输出参数,我右边的参数应该是写错了,,因为调用这个函数返回的是Output字符,而不是@count应该怎么写呢?
};
DataTable dt = sqlhelper.ExecuteQuery("procVoteItemsShow", para, CommandType.StoredProcedure,out count);
return dt;
}
这个是被调用的函数,SqlHelper public DataTable ExecuteQuery(string cmdText, SqlParameter[] para, CommandType ct,out string count)
{
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand(cmdText, GetConn());
cmd.Parameters.AddRange(para);
cmd.CommandType = ct;
SqlDataReader sdr = cmd.ExecuteReader();
count = cmd.Parameters["@count"].Value.ToString(); //这里接收@count的值可否?为什么这个count变量返回的是Output字符?,,,
dt.Load(sdr);
sdr.Close();
}
catch (Exception ex)
{ throw ex;
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
return dt;
}
{
SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@ID",ID),
new SqlParameter("@count",ParameterDirection.Output) //这个@count是存储过程中的输出参数,我右边的参数应该是写错了,,因为调用这个函数返回的是Output字符,而不是@count应该怎么写呢?
};
DataTable dt = sqlhelper.ExecuteQuery("procVoteItemsShow", para, CommandType.StoredProcedure,out count);
return dt;
}
这个是被调用的函数,SqlHelper public DataTable ExecuteQuery(string cmdText, SqlParameter[] para, CommandType ct,out string count)
{
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand(cmdText, GetConn());
cmd.Parameters.AddRange(para);
cmd.CommandType = ct;
SqlDataReader sdr = cmd.ExecuteReader();
count = cmd.Parameters["@count"].Value.ToString(); //这里接收@count的值可否?为什么这个count变量返回的是Output字符?,,,
dt.Load(sdr);
sdr.Close();
}
catch (Exception ex)
{ throw ex;
}
finally
{
if (conn.State == ConnectionState.Open) conn.Close();
}
return dt;
}
param.Direction = ParameterDirection.InputOutput;
list.Add(param);
dt.Load(sdr);
sdr.Close();=>
dt.Load(sdr);
sdr.Close();
count = cmd.Parameters["@count"].Value.ToString();
public static 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;
}
/// <summary>
/// 传入返回值参数
/// </summary>
/// <param name="ParamName">存储过程名称</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新的 parameter 对象</returns>
public static SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
new SqlParameter("@ID",ID),
new SqlParameter("@count",ParameterDirection.Output) //这个是不是这样写呀?
};
如果不是,那要怎么写呢?
new SqlParameter("@ID",ID),
new SqlParameter("@count",ParameterDirection.Output)
}; =>SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@ID",ID),
new SqlParameter("@count",count)
};
para[1].Direction = ParameterDirection.Output;
new SqlParameter("@ID",ID),
new SqlParameter("@count",count)
};
para[1].Direction = ParameterDirection.Output;
new SqlParameter("@ID",ID),
new SqlParameter("@count",count)
};
para[1].Direction = ParameterDirection.Output;
改成这个后都提示“未将对象引用设置到对象的实例。”不知道哪错了,,而且output的参数应该不用传入的吧,,?
new SqlParameter("@ID",ID),
new SqlParameter("@count",count)
};
para[1].Direction = ParameterDirection.Output;
給它個null值。。
SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@ID",ID),
new SqlParameter("@count",null)
};
para[1].Direction = ParameterDirection.Output;
在这个调用函数中没有赋值呀,,值在Sqlhelper类中赋了,,用output传出的呀,,
count = cmd.Parameters["@count"].Value.ToString();
SqlParameter[] parameters = {
new SqlParameter("@pageIndex", SqlDbType.Int),
new SqlParameter("@pageSize", SqlDbType.Int),
new SqlParameter("@sqlStr", SqlDbType.NVarChar,800),
new SqlParameter("@orderFiled", SqlDbType.VarChar,50),
new SqlParameter("@recordsCount", SqlDbType.Int),
};
parameters[0].Value = pageIndex;
parameters[1].Value = pageSize;
parameters[2].Value = sqlStr;
parameters[3].Value = orderFiled;
parameters[4].Direction = ParameterDirection.Output;
new SqlParameter("@ID",SqlDbType.Int),
new SqlParameter("@count",SqlDbType.Int) };
para[0].Value = ID;
para[1].Direction = ParameterDirection.Output;
提示System.NullReferenceException:未将对象引用设置到对象的实例,我第一次使用output来输出参数,不懂呀,,不知道哪错了,,……
@ID int,
@count int = 0 output
as
begin
select sum([Count]) from Vote where Sid = @ID
select ID,Title,[Count],Date from Vote where Sid = @ID
end
GO
这个是存储过程
new SqlParameter("@ID",ID),
new SqlParameter("@count",SqlDbType.Int)
};
para[1].Direction = ParameterDirection.Output;
new SqlParameter("@ID",ID),
new SqlParameter("@count",SqlDbType.Int)
};
para[1].Direction = ParameterDirection.Output;
辛苦大家了,,这个调用时Sqlhelper类里还是抛出异常,提示System.NullReferenceException: 未将对象引用设置到对象的实例。这一般是什么错误呢,我是新手,如果那位方便的话我把文件发过来,帮我改改可以否?