今天在信息發布功能時出現了一個怪異的錯誤(時而出錯,時而不會):
System.Data.SqlClient.SqlException: 當前命令發生了嚴重錯誤。應放棄任何可能產生的結果。
>>具體的錯誤提示如下所示:
System.Data.SqlClient.SqlException: 當前命令發生了嚴重錯誤。應放棄任何可能產生的結果。 當前命令發生了嚴重錯誤。應放棄任何可能產生的結果。 在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) 在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) 在 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 在 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) 在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) 在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在 Microsoft.Practices.EnterpriseLibrary.Data.Database.DoExecuteNonQuery(DbCommand command) 在 Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteNonQuery(DbCommand command) 在 Core.DAL.Article.Update(Article model) 位置 E:\Works\Portal\Core\DAL\Article.cs:行號 149 在 Core.BLL.Article.Update(Article model) 位置 E:\Works\Portal\Core\BLL\Article.cs:行號 37 在 LWDocService.Portal.Admin.ArticleEdit.btnSubmit_Click(Object sender, EventArgs e) 位置 E:\Works\Portal\Admin\ArticleEdit.aspx.cs:行號 86 在 System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
>>具體經驗判斷應該是字段長度的問題,但經網上搜索有些說是沒有安裝SQL Server 2000 SP1補丁導致的,屬於SQL Server的一個bug,但我將數據庫附加到MS SQL Server 2005然后再運行時還是出現錯誤了,而這次的錯誤提示卻與SQL Server 2000不同:
System.Data.SqlClient.SqlException: 傳入的表格格式數據流(TDS)遠程過程調用(RPC)協議流不正確。參數 15 ("@Content"): 數據類型 0xA7 的數據長度或元數據長度無效。
>>從錯誤的提示上看應該可以比較直觀地判斷出是字段Content的長度問題,而為什么SQL Server 2000與SQL Server 2005不同的數據庫版本竟會有不同的錯誤提示呢?大家有興趣或知道的,請告知 :),在這里老林只想解決出現的問題.....
>>好了,知道問題的根源解決就不困難了,
原來的代碼:
db.AddInParameter(dbCommand, "Content", DbType.AnsiString, model.Content);
改為現在的代碼:
db.AddInParameter(dbCommand, "Content", DbType.String, model.Content);
>>那么DbType.AnsiString與DbType.String有任何區別呢?
經查MSDN資料,https://msdn.microsoft.com/zh-cn/library/system.data.dbtype%28VS.80%29.aspx?f=255&MSPPError=-2147217396:
AnsiString——非 Unicode 字符的可變長度流,范圍在 1 到 8,000 個字符之間。
String——表示 Unicode 字符串的類型。
>>大家看到了嗎?AnsiString最大的范圍是在8000個字符之內,如果你的文章內容太多那將可能出錯誤。
原文鏈接:SqlException 當前命令發生了嚴重錯誤 應放棄任何可能產生的結果
>>小廣告: 八爪魚招標網 大家多給意見哦