转换为INT错误而无需使用INT



i有一个C#代码,它为我抛出了一个带有消息错误将数据类型VARCHAR转换为INT的SQLEXCEPTION。有趣的是,我的代码中没有任何转换或INT变量。我的存储过程可以通过SSM自己工作正常。

这是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 \LocalDevRandomWebTest_Replacement_OutlookVBTest_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

我知道这将是愚蠢的,我需要使用sqldbtype而不是dbtype。现在正常工作

相关内容

  • 没有找到相关文章

最新更新