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.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...