请帮帮我:存储过程调用问题

jiangseraph 2009-11-23 09:50:21

private static DataSet runMultiRowSelect(string sqlCommand, Hashtable parameters, string srcTable)
{
SqlDataAdapter oda;
DataSet ds;
SqlCommand myCmd;

using (SqlConnection conn = GetUnopenedConnection())
{
using (oda = new SqlDataAdapter(sqlCommand,conn))
try
{
conn.Open();

myCmd = new SqlCommand(sqlCommand, conn);
myCmd.CommandType = CommandType.StoredProcedure;

// Set up parameters
IDictionaryEnumerator htEnumerator = parameters.GetEnumerator();
while (htEnumerator.MoveNext())
{
if (log.IsDebugEnabled) log.Debug("About to add parameter with name of [" + htEnumerator.Key.ToString()
+ "] and value of [" + htEnumerator.Value + "].");
myCmd.Parameters.AddWithValueAdd(htEnumerator.Key.ToString(), htEnumerator.Value); /*这里可以看到Hashtable有值:Key值: @userID; Value值:100 */
}

if (log.IsInfoEnabled) log.Info("About to run store procedure of {" + sqlCommand + "}.");
{
ds = new DataSet();
oda.SelectCommand.CommandTimeout = Int32.Parse(Properties.Settings.Default.GlobalDatabaseTimeout);
oda.Fill(ds, srcTable);
}
if (log.IsDebugEnabled) log.Debug("Records successfully read.");
if (log.IsInfoEnabled) log.Info("Method exiting");
return (ds); /*程序在这里运行抛出异常:Procedure or function 'XXX' expects parameter '@userID', which was not supplied.*/
}
catch (Exception e)
{
string errorMsg = "Got exception of {" + e.Message + "} while trying to run SQL statement of [" + sqlCommand + "].";
if (log.IsWarnEnabled) log.Warn(errorMsg);
throw new clsUtil.MSSQLDatabaseException(errorMsg);
}
finally
{
conn.Dispose();
oda.Dispose();
}

}

}
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
caotoulei 2009-11-26
  • 打赏
  • 举报
回复
o? seraph...六翼天使???
jiangseraph 2009-11-23
  • 打赏
  • 举报
回复
谢谢,我太粗心了
这就结贴
soaringbird 2009-11-23
  • 打赏
  • 举报
回复
你没有把myCmd给oda.SelectCommand呀
jiangseraph 2009-11-23
  • 打赏
  • 举报
回复
抱歉,上面的程序没有贴对,更正两行:
myCmd.Parameters.AddWithValue(htEnumerator.Key.ToString(), htEnumerator.Value);

单步跟踪可以看到 Hashtable 的Key值:@userID, Value值:100

oda.Fill(ds, srcTable);

程序这句抛出异常
Procedure or function 'XXX' expects parameter '@userID', which was not supplied.

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧