在vb.net中绑定存储过程的参数

时间:2013-07-11 08:08:15

标签: vb.net sql-server-2008 stored-procedures datagridview

我有存储过程来显示数据库中的数据。使用sql server 2008.我的存储过程如下

USE [RSUPDB]
GO
/****** Object:  StoredProcedure [dbo].[spSPDMonStockProductHarian]    Script Date: 07/11/2013 14:56:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[spSPDMonStockProductHarian]
    -- Add the parameters for the stored procedure here
    @Tanggal    Smalldatetime,
    @WHSID      Int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT  NoReff, ProductID,Qnty , CreatedBy, CreatedDate, 
                      LastUpdatedBy, LastUpdatedDate, Computer, ComputerDate
FROM         tblSPDTrnHasilProduksiDtl
Where CreatedDate=@Tanggal 

END

我使用vb.net调用该存储过程,如下所示

Private Sub CmRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmRefresh.Click
    Dim i As Integer

    Dim strCon1 As String = "server='ip server'; database=database name; user=user;password=password;"
    Dim cmd As SqlCommand = New SqlCommand("spSPDMonStockProductHarian", New SqlConnection(strCon1))
    'cmd.CommandText = "SELECT * FROM tblSPDTrnHasilProduksiDtl"
    With cmd

        .Parameters.Add("@Tanggal", SqlDbType.SmallDateTime).Value = Format(CDate(DateTimePicker1.Value), "dd/MM/yyyy")
        .Parameters.AddWithValue("@WhsID", CboWhs.SelectedValue)
        .Parameters.AddWithValue("@NoReff", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(0).FormattedValue.ToString
        .Parameters.AddWithValue("@ProductID", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(1).FormattedValue.ToString
        .Parameters.AddWithValue("@Qnty", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(2).FormattedValue.ToString
        .Parameters.AddWithValue("@CreatedBy", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(3).FormattedValue.ToString
        .Parameters.AddWithValue("@CreatedDate", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(4).FormattedValue.ToString
        .Parameters.AddWithValue("@LastUpdatedBy", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(5).FormattedValue.ToString
        .Parameters.AddWithValue("@LastUpdatedDate", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(6).FormattedValue.ToString
        .Parameters.AddWithValue("@Computer", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(7).FormattedValue.ToString
        .Parameters.AddWithValue("@Computerdate", SqlDbType.VarChar).Value = Listdata.Rows(i).Cells(8).FormattedValue.ToString
        .Connection.Open()
    End With
    Listdata.DataSource = cmd.ExecuteReader

    Koneksi.Close()
    Koneksi.Close()
End Sub

我执行此程序时遇到的问题仍然是错误消息

  

程序或函数'spSPDMonStockProductHarian'需要参数'@Tanggal',这是未提供的

我没有得到什么问题???这使我的头堆栈所以任何解决方案都会欣赏!!

1 个答案:

答案 0 :(得分:1)

已注释掉

'.Parameters.Add("@Tanggal", SqlDbType.SmallDateTime).Value = Format(CDate(DateTimePicker1.Value), "dd/MM/yyyy")

尝试删除beggining的引用,

说,你的代码会因其他原因而失败。

该过程只需要两个参数不是11,@Tanggal参数应该是数据时而不是格式化为DateTime的字符串,您需要将CommandType属性设置为StoredProcedure。

对您的程序采取更正确的方法可能是

Private Sub CmRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmRefresh.Click 
    Dim strCon1 = "server=serverIP; database=database name; user=user;password=password;"
    Dim cmdText = "spSPDMonStockProductHarian"
    Using con = new SqlConnection(strCon1)
    Using cmd = New SqlCommand(cmdText, con)
       With cmd
           .CommandType = CommandType.StoredProcedure
           .Parameters.Add("@Tanggal", SqlDbType.SmallDateTime).Value = dateTimePicker1.Value
           .Parameters.AddWithValue("@WhsID", CboWhs.SelectedValue)
        End With
         con.Open()
        Listdata.DataSource = cmd.ExecuteReader
    End Using
    End Using
End Sub

我删除了未使用的参数,并将@Tanggal参数作为DateTime传递,而不是作为字符串传递。还添加了Using Statement以正确关闭并释放连接对象