locked
MVC and ADO.Net RRS feed

  • Question

  • User-305496339 posted

    Hi Friends,

    I am trying to do something I would think would be extremely easy. Maybe I have got so used to using the entity framework, but I just want to return data from a stored procedure that receives one parameter usining plain old ADO.NET. I dont know what in the world I could be doing wrong but should I enclose the name of my stored procedure in brackets within the SqlCommand like this var command = new SqlCommand("[dbo].[spGetSSN]", connection) or remove the brackets? And is this how I add a parameter to the stored procedure...... command.Parameters.Add(new SqlParameter("ssn", model.SSN)); or do I need a @ sign before the ssn like @ssn? This seems extremely simplistic but for some reason it's not working. Thanks for any HELP !!!

    Tuesday, April 7, 2020 7:27 PM

Answers

All replies

  • User475983607 posted

    The official ADO.NET docs has examples.

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-code-examples

    I would start by going through the source examples.  If you still need help, share your actual source code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 7, 2020 8:08 PM
  • User-305496339 posted

    This is an example of how my colleague performed a delete operation, but I need to pull some data in. Thanks !!!

    public ActionResult RemoveSSN(RemoveSSN.Models.RemoveSSNModel model)

            {

                string message;

                string status;


                if (ModelState.IsValid)

                {

                    using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[model.connectionString].ConnectionString))

                    using (var command = new SqlCommand("[dbo].[spRemoveSSN]", connection)

                    {

                        CommandType = CommandType.StoredProcedure

                    })

                    {

                        try

                        {

                            connection.Open();

                            command.Parameters.Add(new SqlParameter("ssn", model.SSN));

                            command.ExecuteNonQuery();

                            message = "SSN successfully deleted.";

                            status = "success";

    Tuesday, April 7, 2020 8:56 PM
  • User475983607 posted

    The link in my first post shows how to create parameter queries with code examples.  Your colleague's examples shows how to execute a stored procedure with parameters.   Both  are patterns that you should be able to reuse by substituting your SQL script.

    I think it's a good idea to meet with your colleagues and ask for assistance in this matter. 

    Tuesday, April 7, 2020 9:02 PM
  • User-305496339 posted

    Thank You !!!!

    Tuesday, April 7, 2020 9:21 PM
  • User-719153870 posted

    Hi rkrex,

    If the issue has beedn resolved and @mgebhard's post is helpful, please mark his post as the answer, this could help those who met the same issue to easily find the solution.

    Thanks!

    Best Regard,

    Yang Shen

    Wednesday, April 8, 2020 2:36 AM