加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 19:37:07 所属栏目:百科 来源:网络整理
导读:我试图在后面的代码中执行asp.net中的存储过程.我试图传递的参数是strErrorMessage,其中包含值“传输无法连接到服务器.”. 执行查询时的错误消息是:传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确.参数1(“@ errMessage”):数据类型0xE7具有无效的数
我试图在后面的代码中执行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("rn","; ");
            strMessage = strMessage.Replace("   ","");

            strStackTrace = strStackTrace.Replace("rn","; ");
            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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读