使用存储过程搜索数据

时间:2016-11-26 05:07:25

标签: sql-server stored-procedures

尝试执行存储过程时收到错误。

我有一个SQL Server数据库,存储过程如下所示:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Search_Licenses_By_Number]
    @LicenseNumber int
AS
BEGIN
    SELECT 
        LicenseNumber, CentralRegistryNumber, LicenseNameArabic, 
        LicenseNameEnglish, LegalForm, ExpiryDate, LicenseOwner, 
        LicenseOwnerNationality, LicenseType, LicenseActivities, 
        LicenseAddress, Phone, Fax, Email, LicenseStatus 
    FROM
        LicensesTbl 
    WHERE 
        LicenseNumber = @LicenseNumber
END

在Vb中,我有数据层和代码,如下所示:

  Friend Function Search_Licenses_By_Number(ByRef cmd As SqlCommand, ByRef dr As SqlDataReader, ByVal LicenseNumber As Integer)
            Dim retval As Integer
            cmd = New SqlCommand("Search_Licenses_By_Number")
            cmd.Parameters.Add("@LicenseNumber", LicenseNumber, SqlDbType.Int)
            cmd.Parameters.Add("@CentralRegistryNumber", SqlDbType.Int)
            cmd.Parameters.Add("@LicenseNameArabic", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseNameEnglish", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LegalForm", SqlDbType.NVarChar)
            cmd.Parameters.Add("@ExpiryDate", SqlDbType.Date)
            cmd.Parameters.Add("@LicenseOwner", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseOwnerNationality", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseType", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseActivities", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseAddress", SqlDbType.NVarChar)
            cmd.Parameters.Add("@Phone", SqlDbType.NVarChar)
            cmd.Parameters.Add("@Fax", SqlDbType.NVarChar)
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseStatus", SqlDbType.NVarChar)

            retval = dm.executeReader(cmd, dr)

            Return retval
        End Function

我还有一个业务层和代码:

 Public Function Search_Licenses_By_Number(ByVal LicenseNumber As Integer) As Integer
            p.Search_Licenses_By_Number(cmd, dr, LicenseNumber)
            Return p.Search_Licenses_By_Number(cmd, dr, LicenseNumber)
        End Function

我的表单包含14个文本框和1个日期时间选择器和1个按钮

我想要的是按下搜索按钮时显示文本框中的记录取决于在textbox1中输入的LicenseNumber

按钮代码如下:

Dim retval As Integer = p.Search_Licenses_By_Number(txtLicensesNumber.Text)
        txtLicensesNumber.Text = p.Search_Licenses_By_Number("LicenseNumber")
        CentralRegistryNumber.Text = p.Search_Licenses_By_Number("CentralRegistryNumber")
        txtLicensesNameArabic.Text = p.Search_Licenses_By_Number("LicenseNameArabic")
        txtLicensesNameEnglish.Text = p.Search_Licenses_By_Number("LicenseNameEnglish")
        LegalForm.Text = p.Search_Licenses_By_Number("LegalForm")
        ExpiryDate.Value = p.Search_Licenses_By_Number("ExpiryDate").ToString
        LicenseOwner.Text = p.Search_Licenses_By_Number("LicenseOwner")
        LicenseOwnerNationality.Text = p.Search_Licenses_By_Number("LicenseOwnerNationality")
        txtLicensesType.Text = p.Search_Licenses_By_Number("LicenseType")
        LicenseActivities.Text = p.Search_Licenses_By_Number("LicenseActivities")
        txtLicensesAddress.Text = p.Search_Licenses_By_Number("LicenseAddress")
        LicensePhone.Text = p.Search_Licenses_By_Number("Phone")
        LicenseFax.Text = p.Search_Licenses_By_Number("Fax")
        LicenseEmail.Text = p.Search_Licenses_By_Number("Email")
        LicStatus.Text = p.Search_Licenses_By_Number("LicenseStatus")

我得到的错误是

  

程序Search_Licenses_By_Number没有提供参数和参数。

亲切的问候 塞勒姆

3 个答案:

答案 0 :(得分:0)

数据库中的过程只定义了一个参数,并且您提供了15个参数。 在SP中定义所有参数,如下所示

 USE [MPRS]
GO

/****** Object:  StoredProcedure [dbo].[Search_Licenses_By_Number]    Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Search_Licenses_By_Number]
@LicenseNumber int,
 @CentralRegistryNumber int,
         @LicenseNameArabic nvarchar(max), 
        @LicenseNameEnglish nvarchar(max), 
        @LegalForm nvarchar(max),
        @ExpiryDate datetime,
        @LicenseOwner nvarchar(max),
        @LicenseOwnerNationality nvarchar(max),
        @LicenseType nvarchar(max),
        @LicenseActivities nvarchar(max),
        @LicenseAddress nvarchar(max),
        @Phone nvarchar(max), 
        @Fax nvarchar(max), 
        @Email nvarchar(max), 
        @LicenseStatus nvarchar(max),
as

begin

Select LicenseNumber,CentralRegistryNumber, LicenseNameArabic, `LicenseNameEnglish, LegalForm, ExpiryDate, LicenseOwner, LicenseOwnerNationality, LicenseType, LicenseActivities, LicenseAddress, Phone, Fax, Em`ail, LicenseStatus from LicensesTbl 
Where LicenseNumber = @LicenseNumber
End

答案 1 :(得分:0)

cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber)
            cmd.Parameters.Add("@CentralRegistryNumber", SqlDbType.Int)
            cmd.Parameters.Add("@LicenseNameArabic", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseNameEnglish", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LegalForm", SqlDbType.NVarChar)
            cmd.Parameters.Add("@ExpiryDate", SqlDbType.Date)
            cmd.Parameters.Add("@LicenseOwner", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseOwnerNationality", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseType", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseActivities", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseAddress", SqlDbType.NVarChar)
            cmd.Parameters.Add("@Phone", SqlDbType.NVarChar)
            cmd.Parameters.Add("@Fax", SqlDbType.NVarChar)
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar)
            cmd.Parameters.Add("@LicenseStatus", SqlDbType.NVarChar)

答案 2 :(得分:0)

避免使用select,因为这可以返回多行, 在返回行/行作为结果集时,始终选择“Distinct Select”。 尝试使用表值函数调用。 或者这样做如下所示

    SqlConnection connection = new SqlConnection(ConnectionString);

command = new SqlCommand("[dbo].[Search_Licenses_By_Number]", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
reader = command.ExecuteReader();

List<Test> TestList = new List<Test>();
Test test;

while (reader.Read())
{
    test = new Test();
    test.LicenseNumber = int.Parse(reader["LicenseNumber"].ToString());

test.CentralRegistryNumber = int.Parse(reader["CentralRegistryNumber"].ToString());
test.LicenseNameArabic = int.Parse(reader["LicenseNameArabic"].ToString());
 test.LicenseNameEnglish = int.Parse(reader["LicenseNameEnglish"].ToString());
 test.LegalForm = int.Parse(reader["LegalForm"].ToString());
 test.ExpiryDate = int.Parse(reader["ExpiryDate "].ToString());
 test.LicenseOwner= int.Parse(reader["LicenseOwner"].ToString());
 test.LicenseOwnerNationality= int.Parse(reader["LicenseOwnerNationality"].ToString());
 test.LicenseType= int.Parse(reader["LicenseType"].ToString());
 test.LicenseActivities= int.Parse(reader["LicenseActivities"].ToString());
 test.LicenseAddress= int.Parse(reader["LicenseAddress"].ToString());
 test.Phone= int.Parse(reader["Phone"].ToString());
 test.Fax= int.Parse(reader["Fax"].ToString());
 test.Email= int.Parse(reader["Email"].ToString());
 test.LicenseStatus= int.Parse(reader["LicenseStatus"].ToString());

    TestList.Add(test);
}
//sampling it in grid, you may use this data list as per your requirement
gvGrid.DataSource = TestList;
gvGrid.DataBind();