0

我从存储过程(在 SQL Server 2008 中)返回一个静态字符串,如下所示:

select 'abcdefgh.........xyz'

如果静态字符串长度超过某个限制(例如:8kb),则仅将部分字符串(例如:7kb)返回给 .net 应用程序。

尽管我尝试了不同的方式,例如将静态字符串分配给varchar(max)和选择变量,但仍然只返回部分字符串。

我应该返回最大为 5mb 的完整字符串。所以,主要担心:

  1. 我可以从存储过程返回的最大字符串长度是多少
  2. 如何将 5 mb 字符串从存储过程返回到 .net 应用程序。

我请求有人可以帮助我解决这个问题。请在下面找到代码

 using (SqlCommand command = new SqlCommand(Source.GetExportRecordSP, Connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@CandidateRecordID ", SqlDbType.NVarChar, 32)).Value = record;
                try
                {
                    if (Connection.State != ConnectionState.Open)
                    {
                        Connection.Open();
                    }
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                    if(reader.Read())
                        {
                            xmlRecord = new XmlDocument();
                            xmlRecord.LoadXml(reader.GetString(0));
                        }
                    }
                }
                catch (Exception Ex)
                {
                    Logging.WriteError(string.Format("Error while retrieving the Record \"{0}\" details from Database. Exception: {1} ", Ex.ToString()));
                    throw;
                }               
            }

提前感谢极客。

4

2 回答 2

0

由于您似乎没有使用 OLEDB 连接(有 8k 限制),我认为问题出在您的程序代码中。

或者,您的数据库的兼容版本可能设置为 SQL Server 2008 以外的版本(SQL Server 2000 使用 无法返回超过 8k 的数据GetString())。

于 2012-02-23T07:14:41.653 回答
0

感谢您的支持,我在 http://www.sqlservercentral.com/Forums/Topic350590-145-1.aspx找到了 1 个解决方法

修复是,声明一个变量,并且应该初始化为空字符串并与主字符串连接。

  DECLARE @test varchar(MAX);
set @test =''
 select  @test = @test + '<Invoice>.....'

如果字符串长度<8000,则无需上述方法即可。

谢谢大家。

于 2012-02-23T09:34:52.710 回答