将文本框中的空白文本插入为NULL

时间:2014-01-30 15:49:17

标签: visual-studio stored-procedures vb6 null

如果文本框留空,我正在使用VB6并尝试将NULL值插入数据库。当我这样做时,'NULL'被插入到只有NULL的数据库中。我该如何解决这个问题?

Dim ctl As Control
For Each ctl In Me.Controls
   If TypeOf ctl Is TextBox Then
       If ctl.Text = "" Then
       ctl.Text = "NULL"
       End If
  End If
Next

 Set prm = cmdDlrID.CreateParameter("@ContractNumberField", adVarChar, adParamInput,  50, txtContNum.Text)
 cmdDlrID.Parameters.Append prm 
 Set prm = cmdDlrID.CreateParameter("@ContractSuffix", adVarChar, adParamInput, 50,   txtContSfx.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@CustomerLastName", adVarChar, adParamInput, 50,  txtCustLstNme.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@CustomerFirstName", adVarChar, adParamInput, 50, txtCustFstNme.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@Last6OfVin", adVarChar, adParamInput, 10, txtVin6.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@DealershipName", adVarChar, adParamInput, 100,   cmbDealerName.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@ClaimNumber", adVarChar, adParamInput, 50,   txtClaimNumber.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@PortalClaimNumber", adVarChar, adParamInput, 50, txtPortalClaimNum.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@RONumber", adVarChar, adParamInput, 50, txtRONumber.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@ContractID", adVarChar, adParamInput, 50, txtContCode.Text)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@LookUpUser", adVarChar, adParamInput, 50,  username)
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@LookUpTime", adVarChar, adParamInput, 50, "CurrentDate")
 cmdDlrID.Parameters.Append prm
 Set prm = cmdDlrID.CreateParameter("@Department", adVarChar, adParamInput, 50, "Department")
 cmdDlrID.Parameters.Append prm


 cmdDlrID.Execute

存储过程

 ALTER PROCEDURE [dbo].[InsertLookupLog] 
-- Add the parameters for the stored procedure here
@ContractNumberField varchar(50) ,
@ContractSuffix varchar(50),
@CustomerLastName varchar(50), 
@CustomerFirstName varchar(50),
@Last6ofVIN varchar(10), 
@DealerShipName varchar(100),
@ClaimNumber varchar(50), 
@PortalClaimNumber varchar(50),
@RONumber varchar(50), 
@ContractId varchar(50),
@LookUpUser varchar(50), 
@LookUpTime varchar(50),
@Department varchar(50)
 AS
 BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Insert Into tblContractLookupLog (ContractNumberField,ContractSuffix, CustomerLastName, CustomerFirstName,Last6ofVin,DealershipName,ClaimNumber, PortalClaimNumber, RONumber, ContractID, LookUpUser,LookUpTime,Department) Values (@ContractNumberField,@ContractSuffix,@CustomerLastName,@CustomerFirstName,@Last6ofVin,@DealershipName,@ClaimNumber,@PortalClaimNumber,@RONumber, @ContractID, @LookUpUser,@LookUpTime,@Department)
 END

2 个答案:

答案 0 :(得分:2)

此处的问题是您将TextBox设置为字符串文字"NULL"而不是值NULL。您需要使用String,而不是使用Nothing。您可以将其抽象为函数

Function GetDbValue(ByVal tb) 
  If tb.Text = "" Then
    Return Nothing
  Else
    Return tb.Text  
  End If
End Function

然后删除For循环并更改参数创建代码,如下所示

Set prm = cmdDlrID.CreateParameter("@ContractNumberField", adVarChar, adParamInput,  50, GetDbValue(txtContNum)

注意:我不是100%确定Nothing将转换为VB6中的NULL DB值,但我很确定是这种情况。使用字符串文字"NULL"肯定无法正常工作

答案 1 :(得分:0)

由于您使用的是存储过程,因此如果值为空字符串,则可以修改插入null的过程。像这样:

Insert Into tblContractLookupLog (ContractNumberField,More columns here) 
Values (NullIf(@ContractNumberField, ''),More columns here)

如果两个参数的值相同,NullIf函数将返回NULL。在这种情况下,如果@ContractNumberField有一个空字符串,则NULLIF函数将返回NULL。

对存储过程进行此更改后,只需将空字符串传递给存储过程即可。