简体   繁体   中英

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. 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. 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.

F1 F1

you will have to pass a parameter using out / ref keyword and parameters

As yo are using TableAdapters, you need to select the storedproceedure rather than a query for this operation.

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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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