2017-04-18 119 views
0

我有一個C#代碼,它引發了一個SQLException,它帶有消息Error將數據類型varchar轉換爲int。有趣的是我沒有在我的代碼中有任何轉換或INT變量。我的存儲過程通過SSMS獨立運行。不使用int轉換爲Int錯誤

這裏是C#位

 string[] envData = new string[4]; 
     string dbCon = ""; 
     dbCon = "Database=" + "TestData"; 
     dbCon = dbCon + ";Server=" + "DEV"; 
     dbCon = dbCon + ";Trusted_Connection=Yes;"; 

     using (SqlConnection Con = new SqlConnection(dbCon)) 
     { 
      Con.Open(); 
      //using (SqlTransaction tr = Con.BeginTransaction("GetEnvironmentInfo")) 
      //{ 
      string spEnvironmentInfo = @"ELog.GetEnvironmentInfo"; 
      SqlCommand cmd = new SqlCommand(spEnvironmentInfo, Con); 
      cmd.CommandType = CommandType.StoredProcedure; 

      cmd.Parameters.Add(new SqlParameter("@oElogBaseDir", DbType.String)); 
      cmd.Parameters["@oElogBaseDir"].Direction = ParameterDirection.Output; 

      cmd.Parameters.Add(new SqlParameter("@oElogFinancialMonthDir", DbType.String)); 
      cmd.Parameters["@oElogFinancialMonthDir"].Direction = ParameterDirection.Output; 

      cmd.Parameters.Add(new SqlParameter("@oElogFinancialYear", DbType.String)); 
      cmd.Parameters["@oElogFinancialYear"].Direction = ParameterDirection.Output; 

      cmd.Parameters.Add(new SqlParameter("@oElogEmailAccount", DbType.String)); 
      cmd.Parameters["@oElogEmailAccount"].Direction = ParameterDirection.Output; 



      try 
      { 

       cmd.ExecuteNonQuery(); 
       envData[0] = cmd.Parameters["@oElogBaseDir"].Value.ToString(); 
       envData[1] = cmd.Parameters["@oElogFinancialMonthDir"].Value.ToString(); 
       envData[2] = cmd.Parameters["@oElogFinancialYear"].Value.ToString(); 
       envData[3] = cmd.Parameters["@oElogEmailAccount"].Value.ToString(); 

       //tr.Commit(); 
      } 
      catch (System.Exception ex) 
      { 
       //tr.Rollback("GetEnvironmentInfo"); 
       throw ex; 
       //return null; 
      } 
      //} 

     } 

     Response.Write(envData[0].ToString()); 
     Response.Write(envData[1].ToString()); 
     Response.Write(envData[2].ToString()); 
     Response.Write(envData[3].ToString()); 

這裏是SQL語句組成部分

ALTER PROCEDURE [ELog].[GetEnvironmentInfo] 
     @oELogBaseDir   VARCHAR(MAX) OUTPUT, 
     @oELogFinancialMonthDir VARCHAR(MAX) OUTPUT, 
     @oELogFinancialYear  VARCHAR(MAX) OUTPUT, 
     @oElogEmailAccount  VARCHAR(MAX) OUTPUT 
AS 
    BEGIN 




     SELECT 
       @oELogBaseDir = e.ELogBaseDir, 
       @oELogFinancialMonthDir = e.ELogFinancialMonthDir, 
       @oELogFinancialYear = e.ELogFinancialYear, 
       @oElogEmailAccount = e.ElogEmailAccount 
     FROM SSISMeta.Environment e 
     WHERE e.SQLServerInstance = @@SERVERNAME 
    END 

這裏是堆棧跟蹤

[SqlException (0x80131904): Error converting data type varchar to int.] 
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2442126 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5736904 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628 
    System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731 
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +225 
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375 
    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +337 
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +280 
    WebTest_Replacement_OutlookVB.Test_Home.btnReadEmail_Click(Object sender, EventArgs e) in \\LocalDev\Random\WebTest_Replacement_OutlookVB\Test_Home.aspx.cs:70 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9692746 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3562 

表架構

SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [SSISMeta].[Environment] (
     [SK_EnvironmentID]   [smallint] IDENTITY(1, 1) NOT NULL, 
     [IsProduction]    [bit] NOT NULL, 
     [SQLServerInstance]   [varchar](100) COLLATE Latin1_General_CI_AS NOT NULL, 
     [BaseDirPath]    [varchar](4000) COLLATE Latin1_General_CI_AS NOT NULL, 
     [CreatedTimeStamp]   [datetime] NOT NULL, 
     [LastUpdatedTimeStamp]  [datetime] NOT NULL, 
     [ModifiedBYUser]   [varchar](100) COLLATE Latin1_General_CI_AS NOT NULL, 
     [SharedBaseDir]    [varchar](4000) COLLATE Latin1_General_CI_AS NOT NULL, 
     [SharedT2ODir]    [varchar](4000) COLLATE Latin1_General_CI_AS NULL, 
     [ELogBaseDir]    [varchar](4000) COLLATE Latin1_General_CI_AS NOT NULL, 
     [ELogFinancialMonthDir]  [varchar](250) COLLATE Latin1_General_CI_AS NOT NULL, 
     [ELogFinancialYear]   [varchar](250) COLLATE Latin1_General_CI_AS NOT NULL, 
     [ElogEmailAccount]   [varchar](250) COLLATE Latin1_General_CI_AS NOT NULL, 
     CONSTRAINT [PK_Environment] 
     PRIMARY KEY 
     CLUSTERED 
     ([SK_EnvironmentID]) 
    ON [SSISMeta_Data_Fg] 
) ON [SSISMeta_Data_Fg] 
GO 

ALTER TABLE [SSISMeta].[Environment] 
    ADD 
    CONSTRAINT [DF_Environment_CreatedTimestamp] 
    DEFAULT (getdate()) FOR [CreatedTimeStamp] 
GO 
ALTER TABLE [SSISMeta].[Environment] 
    ADD 
    CONSTRAINT [DF_Environment_LastUpdatedTimestamp] 
    DEFAULT (getdate()) FOR [LastUpdatedTimeStamp] 
GO 

ALTER TABLE [SSISMeta].[Environment] 
    ADD 
    CONSTRAINT [DF_Environment_ModifiedByUser] 
    DEFAULT (suser_sname()) FOR [ModifiedBYUser] 
GO 
ALTER TABLE [SSISMeta].[Environment] SET (LOCK_ESCALATION = TABLE) 
GO 
+5

這是一個SQL錯誤,而不是C#錯誤。 e.ELogFinancialYear的數據類型是什麼? – DavidG

+0

存儲過程自己在SQL中正常工作 – mouliin

+0

如果要重新拋出異常,請使用'throw'並且noe'扔前';' - 那麼你鬆散的堆棧跟蹤。再次運行它並從拋出的異常粘貼堆棧跟蹤。 – TcKs

回答

0

我知道這會變得愚蠢,我需要使用SqlDbType而不是DbType。 現在正常工作