将数据类型 int 转换为 nvarchar 时出错

Posted

技术标签:

【中文标题】将数据类型 int 转换为 nvarchar 时出错【英文标题】:Error converting data type int to nvarchar 【发布时间】:2013-03-26 09:56:48 【问题描述】:

我收到一个错误-“将数据类型“int”转换为nvarchar 时出错

这是 VB6 代码:

Private Sub Form_Load()
On Error GoTo ErrDoor
Dim lvRS_Status As New ADODB.Recordset
    If gFn_OpenCnn = True Then
    If lvRS_Status.State = adStateOpen Then lvRS_Status.Close
      lvRS_Status.Open "Select Sts_StatusID,Sts_StatusDesc from Status_mst", gvCnn_CBSBSDB, adOpenDynamic, adLockOptimistic, adCmdText
      If lvRS_Status.RecordCount <> 0 Then
        Dcbo_Status.ListField = "Sts_StatusDesc"
        Dcbo_Status.BoundColumn = "Sts_StatusID"
        Set Dcbo_Status.RowSource = lvRS_Status
      End If

    End If
On Error GoTo 0
Exit Sub
ErrDoor:
    MsgBox Error$, vbExclamation
End Sub

Private Sub Cmd_OK_Click()
On Error GoTo ErrDoor
Dim lvStr_Month As String
Dim lvStr_Year As String
Dim lvStr_SelectionFormula As String
Dim lvStr_StationName As String
Dim lvDbl_CurrentSessionID As Double
lvStr_SelectionFormula = ""
 lvDbl_CurrentSessionID = Int((2000 * Rnd) + 1000)

    gvCnn_CBSBSDB.BeginTrans
    gvCnn_CBSBSDB.Execute "Exec DBPrc_SundryDebrorAgewiseDetails " & lvDbl_CurrentSessionID & ", '" & Dcbo_Status.BoundText & "', " & Cbo_SelectMonth.ListIndex + 1 & ", " & Txt_SelectYear.Text & ""
    gvCnn_CBSBSDB.CommitTrans


    Crpt_SundryDebtor.Reset
    Crpt_SundryDebtor.WindowState = crptMaximized


    Crpt_SundryDebtor.ReportFileName = App.Path & "\sundrydebtor_agewise.rpt"
    Crpt_SundryDebtor.DiscardSavedData = True
   If Trim(Cbo_SelectMonth.Text) <> "" Then
       lvStr_Month = "SundryDebtors_AgeWise.lvStr_Month = " & Cbo_SelectMonth.ItemData(Cbo_SelectMonth.ListIndex)
       lvStr_SelectionFormula = lvStr_Month
   Else
       MsgBox "Select the month ", vbInformation, "Sundry Debtor Age wise"
       Exit Sub
   End If


   If Trim(Txt_SelectYear.Text) <> "" Then

           lvStr_Month = lvStr_SelectionFormula + " And SundryDebtors_AgeWise.Sde_Year  =" & Txt_SelectYear.Text
           lvStr_SelectionFormula = lvStr_Month

    End If


    If Trim(Dcbo_Status.Text) <> "" Then
        If lvStr_SelectionFormula <> "" Then
           lvStr_Month = lvStr_SelectionFormula + " And Status_Mst.Sts_StatusID =" & val(Dcbo_Status.BoundText) & ""
           lvStr_SelectionFormula = lvStr_Month
        End If
    End If


    Crpt_SundryDebtor.SelectionFormula = "Reporter.Session_ID = " & lvDbl_CurrentSessionID & ""
        Crpt_SundryDebtor.Connect = gvCnn_CBSBSDB
        Crpt_SundryDebtor.DiscardSavedData = True
        Crpt_SundryDebtor.WindowShowRefreshBtn = True
        Crpt_SundryDebtor.WindowShowPrintSetupBtn = True
        Crpt_SundryDebtor.Destination = crptToWindow
        Crpt_SundryDebtor.Action = 1



On Error GoTo 0
Exit Sub
Resume
ErrDoor:
    MsgBox Error$, vbExclamation

End Sub

这是存储过程:

CREATE PROCEDURE DBPrc_SundryDebrorAgewiseDetails(
  @SessionId as int,
  @StatusId as int,
  @Month as nvarchar,
  @Year as nvarchar)
as 
begin
if @StatusId=''
begin
    SELECT
      @SessionId,
      Sts_StatusDesc, Agt_AgentName,Sde_OutstandingAsOnDate,
      Sde_AmountLessThanSixMonths,
      Sde_AmountBetweenSixMonthstoOneYear,Sde_AmountBetweenOneToTwoYear,
      Sde_AmountBetweenTwoToThreeYears,Sde_AmountMoreThanThreeYears,
      Sde_TotalAdChanrges,Sde_TotalProgressiveInterest,Sde_TotalOutstandingAsOnDate,
      Sde_Remark, Sde_SundryDebtorCode
    FROM SundryDebtors_AgeWise left join Status_Mst
           on sts_StatusId=Sde_StatusId
         left join Agent_Mst
           on Agt_AgentId=Sde_AgentId
    WHERE SundryDebtors_AgeWise.Sde_Month=@Month 
      and SundryDebtors_AgeWise.Sde_Year=@Year 
    GROUP BY Sts_StatusDesc, Agt_AgentName,Sde_OutstandingAsOnDate,Sde_AmountLessThanSixMonths,Sde_AmountBetweenSixMonthstoOneYear,Sde_AmountBetweenOneToTwoYear,Sde_AmountBetweenTwoToThreeYears,Sde_AmountMoreThanThreeYears,Sde_TotalAdChanrges,Sde_TotalProgressiveInterest,Sde_TotalOutstandingAsOnDate,Sde_Remark,Sde_SundryDebtorCode

End

If @StatusId<>''
Begin

    SELECT
      @SessionId,
      Sts_StatusDesc,Agt_AgentName,Sde_OutstandingAsOnDate,
      Sde_AmountLessThanSixMonths,Sde_AmountBetweenSixMonthstoOneYear,
      Sde_AmountBetweenOneToTwoYear,Sde_AmountBetweenTwoToThreeYears,
      Sde_AmountMoreThanThreeYears,Sde_TotalAdChanrges,Sde_TotalProgressiveInterest,
      Sde_TotalOutstandingAsOnDate,Sde_Remark,Sde_SundryDebtorCode
    FROM SundryDebtors_AgeWise left join Status_Mst
           on sts_StatusId=Sde_StatusId
         left join Agent_Mst
         on Agt_AgentId=Sde_AgentId
    WHERE SundryDebtors_AgeWise.Sde_Month=@Month 
      and SundryDebtors_AgeWise.Sde_Year=@Year 
      and Sts_StatusId=@StatusId
    GROUP BY Sts_StatusDesc, Agt_AgentName,Sde_OutstandingAsOnDate,Sde_AmountLessThanSixMonths,Sde_AmountBetweenSixMonthstoOneYear,Sde_AmountBetweenOneToTwoYear,Sde_AmountBetweenTwoToThreeYears,Sde_AmountMoreThanThreeYears,Sde_TotalAdChanrges,Sde_TotalProgressiveInterest,Sde_TotalOutstandingAsOnDate,Sde_Remark,Sde_SundryDebtorCode
End

End
GO

【问题讨论】:

请朋友帮我解决这个错误 欢迎来到 ***。请花点时间阅读FAQ on how to ask a good question。下一次,请使用摘要作为主题,并将问题放在问题的实际正文中。我已将您的问题编辑为更合适。 【参考方案1】:

你的 SP 参数是一个 int :@StatusId as int,

...但您正在针对空字符串 '' 进行测试:if @StatusId=''

【讨论】:

以上是关于将数据类型 int 转换为 nvarchar 时出错的主要内容,如果未能解决你的问题,请参考以下文章

将 nvarchar 值...转换为数据类型 int 时转换失败

将数据类型 NVarchar 转换为 int 时出错

SQL 异常:将 nvarchar 值“[anyvalue]”转换为数据类型 int 时转换失败

System.Data.SqlClient.SqlException:“将 nvarchar 值 'STORES' 转换为数据类型 int 时转换失败。”

nvarchar 值转换为数据类型为 int 发生语法错误

将 nvarchar 值转换为数据类型 int 和其他 SQL Server 错误时失败