问题描述
参数化查询'(@FromUserIdint,@ToUserIdint,@Messagenvarchar(1),@MessageType'需要参数'@MessageState',但未提供该参数。错误详细信息为:未处理System.Data.SqlClient.SqlExceptionMessage="参数化查询'(@FromUserIdint,@ToUserIdint,@Messagenvarchar(1),@MessageType'需要参数'@MessageState',但未提供该参数。"Source=".NetSqlClientDataProvider"ErrorCode=-2146232060Class=16LineNumber=0Number=8178Procedure=""Server="(local)"State=1StackTrace:在System.Data.SqlClient.SqlConnection.OnError(SqlExceptionexception,BooleanbreakConnection)在System.Data.SqlClient.SqlInternalConnection.OnError(SqlExceptionexception,BooleanbreakConnection)在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObjectstateObj)在System.Data.SqlClient.TdsParser.Run(RunBehaviorrunBehavior,SqlCommandcmdHandler,SqlDataReaderdataStream,BulkCopySimpleResultSetbulkCopyHandler,TdsParserStateObjectstateObj)在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReaderds,RunBehaviorrunBehavior,StringresetOptionsString)在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehaviorcmdBehavior,RunBehaviorrunBehavior,BooleanreturnStream,Booleanasync)在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehaviorcmdBehavior,RunBehaviorrunBehavior,BooleanreturnStream,Stringmethod,DbAsyncResultresult)在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResultresult,StringmethodName,BooleansendToPipe)在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()在DAL.SqlHelper.ExecuteNonQuery(StringconnectionString,CommandTypecmdType,StringcmdText,SqlParameter[]commandParameters)位置D:BOOKC#_codeprojectQQ2010DALSQLHelper.cs:行号54在DAL.MessagesDAL.Insert(MessagesInfoentity)位置D:BOOKC#_codeprojectQQ2010DALMessagesDAL.cs:行号122在BLL.Messages.SendMessage(MessagesInfoentity,EnumMessageTypemessageType)位置D:BOOKC#_codeprojectQQ2010BLLMessages.cs:行号84在MyQQ.frmChat.btnSend_Click(Objectsender,EventArgse)位置D:BOOKC#_codeprojectQQ2010MyQQfrmChat.cs:行号77在System.Windows.Forms.Control.OnClick(EventArgse)在System.Windows.Forms.Button.OnClick(EventArgse)在System.Windows.Forms.Button.OnMouseUp(MouseEventArgsmevent)在System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtonsbutton,Int32clicks)在System.Windows.Forms.Control.WndProc(Message&m)在System.Windows.Forms.ButtonBase.WndProc(Message&m)在System.Windows.Forms.Button.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32dwComponentID,Int32reason,Int32pvLoopData)在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32reason,ApplicationContextcontext)在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32reason,ApplicationContextcontext)在System.Windows.Forms.Application.Run(FormmainForm)在MyQQ.Program.Main()位置D:BOOKC#_codeprojectQQ2010MyQQProgram.cs:行号17在System.AppDomain._nExecuteAssembly(Assemblyassembly,String[]args)在System.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart_Context(Objectstate)在System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,Objectstate)在System.Threading.ThreadHelper.ThreadStart()InnerException:具体代码如下:usingSystem;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Collections;namespaceDAL{publicclassSqlHelper{publicstaticreadonlystringConnString=ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;//HashtabletostorecachedparametersprivatestaticHashtableparmCache=Hashtable.Synchronized(newHashtable());publicstaticintExecuteNonQuery(stringconnectionString,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters){SqlCommandcmd=newSqlCommand();using(SqlConnectionconn=newSqlConnection(connectionString)){PrepareCommand(cmd,conn,null,cmdType,cmdText,commandParameters);intval=cmd.ExecuteNonQuery();cmd.Parameters.Clear();returnval;}}publicstaticintExecuteNonQuery(SqlConnectionconnection,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters){SqlCommandcmd=newSqlCommand();PrepareCommand(cmd,connection,null,cmdType,cmdText,commandParameters);intval=cmd.ExecuteNonQuery();cmd.Parameters.Clear();returnval;}publicstaticintExecuteNonQuery(SqlTransactiontrans,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters){SqlCommandcmd=newSqlCommand();PrepareCommand(cmd,trans.Connection,trans,cmdType,cmdText,commandParameters);intval=cmd.ExecuteNonQuery();cmd.Parameters.Clear();returnval;}publicstaticSqlDataReaderExecuteReader(stringconnectionString,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters){SqlCommandcmd=newSqlCommand();SqlConnectionconn=newSqlConnection(connectionString);try{PrepareCommand(cmd,conn,null,cmdType,cmdText,commandParameters);SqlDataReaderrdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);cmd.Parameters.Clear();returnrdr;}catch{conn.Close();throw;}}publicstaticobjectExecuteScalar(stringconnectionString,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters){SqlCommandcmd=newSqlCommand();using(SqlConnectionconnection=newSqlConnection(connectionString)){PrepareCommand(cmd,connection,null,cmdType,cmdText,commandParameters);objectval=cmd.ExecuteScalar();cmd.Parameters.Clear();returnval;}}publicstaticobjectExecuteScalar(SqlConnectionconnection,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters){SqlCommandcmd=newSqlCommand();PrepareCommand(cmd,connection,null,cmdType,cmdText,commandParameters);objectval=cmd.ExecuteScalar();cmd.Parameters.Clear();returnval;}publicstaticvoidCacheParameters(stringcacheKey,paramsSqlParameter[]commandParameters){parmCache[cacheKey]=commandParameters;}publicstaticSqlParameter[]GetCachedParameters(stringcacheKey){SqlParameter[]cachedParms=(SqlParameter[])parmCache[cacheKey];if(cachedParms==null)returnnull;SqlParameter[]clonedParms=newSqlParameter[cachedParms.Length];for(inti=0,j=cachedParms.Length;i<j;i++)clonedParms[i]=(SqlParameter)((ICloneable)cachedParms[i]).Clone();returnclonedParms;}privatestaticvoidPrepareCommand(SqlCommandcmd,SqlConnectionconn,SqlTransactiontrans,CommandTypecmdType,stringcmdText,SqlParameter[]cmdParms){if(conn.State!=ConnectionState.Open)conn.Open();cmd.Connection=conn;cmd.CommandText=cmdText;if(trans!=null)cmd.Transaction=trans;cmd.CommandType=cmdType;if(cmdParms!=null){foreach(SqlParameterparmincmdParms)cmd.Parameters.Add(parm);}}}}请教高手如何解决?谢谢。
解决方案
解决方案二:
错误提示很明显了,是'需要参数'@MessageState'
解决方案三:
引用1楼caicaihui的回复:
错误提示很明显了,是'需要参数'@MessageState'
就是不知道哪个参数啊。。