简体   繁体   English

C#Winforms-如何将参数传递给SQL Server存储过程

[英]C# Winforms - How to Pass Parameters to SQL Server Stored Procedures

my practice is that i fill use databinded listboxes and using a tablebindingsource and tableadapter. 我的做法是,我填写使用数据绑定列表框并使用tablebindingsource和tableadapter。 Then to get selective records in listbox i build stored procedure and select required records. 然后要获得列表框中的选择性记录,我建立存储过程并选择所需的记录。

But now i want that the stored procedure must take some parameter from some textbox value. 但是现在我希望存储过程必须从某些文本框值中获取一些参数。

like i to implement the following query 像我实现以下查询

string query = "SELECT Patient_ID, Patient_Name FROM Patient WHERE ( Patient_Name LIKE '"+ textbox1.Text +"%' )";

how to do it in a stored procedure. 如何在存储过程中做到这一点。 cuz what i know is that i can only give a query like this in stored procedure 因为我知道的是我只能在存储过程中给出这样的查询

SELECT Patient_ID, Patient_Name FROM dbo.Patient WHERE ( Patient_Name LIKE 'DAV%' )

And then you make a stored procedure and fill the tableadapter with that stored procedure. 然后创建一个存储过程,并用该存储过程填充tableadapter。 like 喜欢

this.accountsTableAdapter.FillBy_I(this.junaidDataSet.Patient);

My knowledge is limited ti Visual Studio 2008's interface and how to do stuff on it. 我的知识仅限于Visual Studio 2008的界面以及如何在界面上进行操作。

F1 F1 F1 F1

you will have to pass a parameter using out / ref keyword and parameters 您将必须使用out / ref关键字和参数传递参数

As yo are using TableAdapters, you need to select the storedproceedure rather than a query for this operation. 在使用TableAdapter时,您需要选择存储过程而不是对该操作的查询。

when you select that, it will recognise the parameters itself. 选择该选项时,它将识别参数本身。

when you call the method over your TableAdapter, which is SelectByName in this case, it is going to be something similar . 当您通过TableAdapter调用方法时(在这种情况下为SelectByName),它将类似于。 Modify accordingly 相应地修改


// your TableAdapter
PatientTableAdapter adapter = new PatientTableAdapter();


// your input and output variables
string name = "somePatientName";
int patientID? = 0;
string returnedName? = "";

// TableAdapter Method, wired to Stored Proceedure
adapter.SelectByName("somePatientName", out patientID, out returnedName);

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM