我有存储过程允许空参数,并在SSMS中正常工作。 。这是SP
ALTER PROCEDURE [dbo].[sp_RepInventorySales]
@SalesOrderNo varchar(9) = null
AS
BEGIN
SELECT SalesOrderNo, ItemCode, ContainerNo, SealNo, ContainerType, OrderQty
FROM SalesOrderDetail
where SalesOrderNo = isnull(@SalesOrderNo, SalesOrderNo )
实际上SP是我的水晶报告的来源。当Me.txtSalesOrderNo.Text非空时,我得到正确的输出。但是,如果textbox为空,则存储过程将参数视为空字符串''。如何将空字符串替换为NULL? 。这是代码VB代码
Private Dset As DataSet
Private Da As SqlDataAdapter
Dim objRpt As New RepInventorySales /*used for crystal report*/
Dset = New DataSet
Da = New SqlDataAdapter("sp_RepInventorySales '" & trim(Me.txtSalesOrderNo.Text) & "' ", Conn_DB)
Da.Fill(Dset)
If Dset.Tables(0).Rows.Count <> 0 Then
objRpt.SetDataSource(Dset.Tables(0))
我尝试在SqlDataAdapter中的查询字符串中使用IIF将空字符串替换为null但是给了我错误
Da = New SqlDataAdapter("sp_RepInventorySales '" & Trim(Me.IIf(txtTextBox.Text Is Nothing, Null, txtTextBox.Text).Text) & "' ", Conn_DB)
这是最终的解决方案。谢谢你的帮助!
IF @SalesOrderNo = ''
SET @SalesOrderNo = NULL
IF @ContainerType = ''
SET @ContainerType = NULL
SELECT SalesOrderNo, ItemCode, ContainerNo, SealNo, ContainerType, OrderQty
FROM SalesOrderDetail
WHERE ( SalesOrderNo = @SalesOrderNo OR @SalesOrderNo IS NULL )
AND
( ContainerType = @ContainerType OR @ContainerType IS NULL )
答案 0 :(得分:1)
改为使用DBNull.Value
。
IF ELSE方法:(在调用 New SqlDataAdapter
之前验证并分配,因此在sql适配器中不需要IIF
,只需分配价值textbox
)
If String.IsNullOrEmpty(txtSalesOrderNo.Text.ToString().Trim) = true Then
txtSalesOrderNo.Text = DBNull.Value
Else
txtSalesOrderNo.Text = txtSalesOrderNo.Text
End If
答案 1 :(得分:1)
您将始终获得空字符串,因为您将参数作为硬编码字符串传递到单引号..."sp_RepInventorySales '" & trim(Me.txtSalesOrderNo.Text) & "'...
正确地将参数添加到SqlDataAdapter.SelectCommand.Parameters
集合,然后您可以使用DBNull.Value
Da = New SqlDataAdapter("sp_RepInventorySales", Conn_DB)
DA.SelectCommand.CommandType = CommandType.StoredProcedure
'Parameter's type need to be Object because DBNull.Value cannot be converted to string
Dim paramValue as Object = DirectCast(DBNull.value, Object)
If String.IsNullOrEmpty(Me.txtSalesOrderNo.Text) = false Then
paramValue = Me.txtSalesOrderNo.Text.Trim()
End If
Dim param As New SqlParameter With {.ParameterName = "@SalesOrderNo",
.SqlDbType = SqlDbType.VarChar,
.Value = paramValue}
DA.SelectCommand.Parameters.Add(param)
Da.Fill(Dset)
答案 2 :(得分:1)
我用解决方案改进了你的方法,你可以试试这个
Sub fun()
Dim Dset As DataSet
Dim Da As PgSqlDataAdapter
Dim strParam As Object
Dim objRpt As New crptAccountlist
Dset = New DataSet
If Trim(txtSalesOrderNo.Text) <> vbNullString Then
strParam = Trim(txtSalesOrderNo.Text)
Else
strParam = DBNull.Value
End If
Da = New PgSqlDataAdapter("", Conn_DB)
With Da
.SelectCommand.CommandType = CommandType.StoredProcedure
.SelectCommand.CommandText = "sp_RepInventorySales"
.SelectCommand.Parameters.AddWithValue("@SalesOrderNo", strParam)
End With
Da.Fill(Dset)
End Sub
请参阅此Demo
答案 3 :(得分:0)
只是将条件放在你的SP上
IF @SalesOrderNo = '' SET @SalesOrderNo = null
IF @ContainerType= '' SET @ContainerType= null
SELECT SalesOrderNo, ItemCode, ContainerNo, SealNo, ContainerType, OrderQty
FROM SalesOrderDetail
where SalesOrderNo = @SalesOrderNo AND ContainerType = @ContainerType