Wednesday, August 18, 2010

How to implement stored procedure using ADO.NET?

There are 2 tables in “GoodCompany” database “Customers” and “Sales”. You have a valid connection string called “MyConnection” with default database set to “GoodCompany”.








“Customers” has “CustID” as primary key/identity and “FullName” field.








“Sales” has “ProdID” as primary key/identity, “ProductName”, and “CustID” that is foreign key to the “Customers” table.





SELECT CustID, FullName, ProdID, ProductName


FROM Customers c, Sales s


WHERE c.CustID = s.CustID





Please do the following using ADO.NET:





Create a Stored Procedure that takes CustID as a parameter and returns all Sales for that customer along with Customer’s name.





Explain how to populate a GridView with this data.





SEE my answer in the Details section... Is is right?How to implement stored procedure using ADO.NET?
Looks correct to me. A little tip: when populating stored procedure parameters, I find this syntax more readable:





command.Parameters.Add(';@CustID';, SqlDbType.NVarChar).Value = custID





This way you can declare the parameter, its type and set its value all on one line.

No comments:

Post a Comment