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 tableadapt开发者_StackOverflow社区er. 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.
F1F1
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
精彩评论