2016-11-26 165 views
0

我遇到了嘗試執行存儲過程的錯誤。使用存儲過程搜索數據

我有一個SQL Server數據庫,存儲過程是這樣的:

​​

在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個日期時間選擇器的形式d 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沒有參數並提供參數。

親切的問候 塞勒姆

回答

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 
+0

謝謝你,但我得到了另一個錯誤過程或函數「Search_Licenses_By_Number」需要參數「@CentralRegistryNumber」,但未提供。我想把文本框中的LicenseNumber,它返回給我所有我想要的數據CentralRegistryNumber,LicenseNameArabic,'LicenseNameEnglish,LegalForm,ExpiryDate,LicenseOwner,LicenseOwnerNationality,LicenseType,LicenseActivities,LicenseAddress,Phone,Fax,Em'ail,LicenseStatus – Salem

+0

分號是第一個和第二個參數之間缺失。檢查更新代碼 –

+0

謝謝阿里,但仍然相同 – Salem

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)

0

避免使用只選擇,因爲這可以返回多行, 總是去「另類選擇」,而返回的行/列的結果組。 嘗試使用表值函數調用。 或像這樣做如下圖所示

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(); 
+0

謝謝你先生IAM不使用C#:( – Salem