执行查询时的错误消息是:传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确.参数1(“@ errMessage”):数据类型0xE7具有无效的数据长度或元数据长度.
使用代码更新
try { ... ... ... } catch (Exception ex) { ClientScript.RegisterStartupScript(GetType(),"alert","alert('Email was not sent - " + ex.Message + "');",true); string strMessage = ex.Message; string strStackTrace = ex.StackTrace; strMessage = strMessage.Replace("\r\n","; "); strMessage = strMessage.Replace(" ",""); strStackTrace = strStackTrace.Replace("\r\n","; "); strStackTrace = strStackTrace.Replace(" ",""); AppErrorLog(strMessage,strStackTrace); return false; } protected void AppErrorLog(string strErrorMessage,string strErrorStackTrace) { sqlConnection conErrLog = new sqlConnection(strConn); string sql = "usp_AppErrorLog_AddRecord"; sqlCommand cmdErrLog = new sqlCommand(sql,conErrLog); conErrLog.open(); try { cmdErrLog.CommandType = CommandType.StoredProcedure; cmdErrLog.Parameters.Add(new sqlParameter("@errMessage",sqlDbType.NVarChar,8000)); cmdErrLog.Parameters["@errMessage"].Value = strErrorMessage; cmdErrLog.Parameters.Add(new sqlParameter("@errStackTrace",8000)); cmdErrLog.Parameters["@errStackTrace"].Value = strErrorStackTrace; cmdErrLog.Parameters.Add(new sqlParameter("@userID",sqlDbType.VarChar,12)); cmdErrLog.Parameters["@userID"].Value = User.Identity.Name; sqlDataAdapter ada = new sqlDataAdapter(cmdErrLog); cmdErrLog.ExecuteNonQuery(); } catch(Exception e) { ClientScript.RegisterStartupScript(GetType(),"alert('AppErrorLog - " + e.Message + "');",true); } finally { conErrLog.Close(); } }
表中的列数据类型是nvarchar(MAX).
任何想法如何解决这个问题?
解决方法
根据这篇文章
When you specify an NVarChar parameter
with sqlParameter.Size between 4001
and 8000,sqlClient will throw the
following exception.The incoming tabular data stream (TDS)
remote procedure call (RPC) protocol
stream is incorrect. Parameter
(“@”): Data type 0xE7
has an invalid data length or Metadata
length.To work around this issue,use one of
the following options:· Set sqlparamter.size
property to -1 to ensure that you are
getting the entire data from the
backend without truncation.· When working with String
DbTypes whose sizes are greater than
4000,explicitly map them to another
sqlDBType like NText instead of using
NVarchar(which also is the default
sqlDBType for strings).· Use a value that is not between 4001 and 8000 for sqlparameter.size.