微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 在asp.net中执行存储过程时出错

我试图在后面的代码中执行asp.net中的存储过程.我试图传递的参数是strErrorMessage,其中包含值“传输无法连接到服务器.”.

执行查询时的错误消息是:传入的表格数据流(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).
任何想法如何解决这个问题?

解决方法

这部分“数据类型0xE7具有无效的数据长度”使我相信参数strErrorMessage被指定为具有比sql Parameter DataType可以处理的数据长度更多的数据长度.

Here是Microsoft支持文章,可能会有所帮助.

根据这篇文章

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐