public DataTable SelectData(string stored_procedure, SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
if (param != null)
{
for (int i = 0; i < param.Length; i++)
{
sqlcmd.Parameters.Add(param[i]);
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
答案 0 :(得分:1)
如错误消息所述,您仅在条件为true时才返回值。如果为假,则不返回任何值(该方法要求该值)。
要使其正常工作,您的代码应如下所示:
public DataTable SelectData(string stored_procedure, SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = stored_procedure;
if (param != null)
{
for (int i = 0; i < param.Length; i++)
{
sqlcmd.Parameters.Add(param[i]);
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
return null; // Or any value you want to return here if the condition is false
}
只需记住在调用方法后检查结果是否为空,以避免任何异常。