• About Morris Development
  • A Focus for Cloud Efficiency
    • Microsoft Azure
    • Amazon Web Services
  • What our Clients Say
  • Our Products
    • PreschoolDB
    • WebinarDB
  • Contact Us

Morris Development

Custom System Development and Integration

October 12, 2013

SQLCommand

Using a stored procedure is the best way to interact with a database.  I’ll use an example from the Dilithium Networks TCAN system (case.aspx).The first thing that we do is to make a new connection.  Since we are using the custom DataLayer module for all database interactions, we already have the connection defined.  Here, as per usual, I’ve created a new datalayer and a new connection object.  All I need to do is to set the connection = to the datalayer’s connection object.  Then, I make a new command object.

The SQLCommand object is a nice clean package.  It has a couple item values you need to set, a couple objects you need to add and a couple functions you need to execute.

For settings, you need to give it the connection, tell it the name of the stored procedure  (CommandText), and tell it that you’re sending a stored procedure, not a text sql statement (CommandType) ,

Stored procedures have parameters.  In this case, we’re updating a QA Test Case’s status.  So, the database needs to know which record and what the new status values are.  Those are our parameters.  In the Stored Procedure in SQL Server, the parameters have @ symbols in front of them and we have to duplicate the names in our code.  For the sake of ease and efficiency, were’ going to use the AddWithValue method to add the parameters.  All that means is that we’re going to say, “the stored procedure paramenter @xyz is equal to my program’s variable abc“.  If you wanted to, you could be very specific with types and sizes, but we aren’t going to go that way unless necessary.

In the code below, you’ll see how we add the parameters to the SQLCommand object.  Once we’ve done that, we can start with the actual execution.  We’ve got the connection values, so we have the location of the db, the username and password.  We’ve got the name of the stored procedure in the database, so we know what we are going to tell the database.  We’ve got the values we are going to send to that stored procedure.  That’s all we need.  Now, just open the connection and use the (ExecuteNonQuery) command to send it.  Then, close the connection and take the entire object out of memory by using the Dispose() command.

Dim cn As SqlConnection

Dim dl As New DataLayer
cn = dl.connection
 Dim cmd As New SqlCommand
 cmd.Connection = cn
 cmd.CommandText = "sp_update_case_status"
 cmd.CommandType = CommandType.StoredProcedure
 With cmd.Parameters
 .AddWithValue("@case_id", case_id)
 .AddWithValue("@case_status", case_status)
 .AddWithValue("@status_AT", status_AT)
 .AddWithValue("@status_REG", status_REG)
 .AddWithValue("@status_SVT", status_SVT)
 .AddWithValue("@status_SIT", status_SIT)
 End With
 cmd.Connection.Open()
 cmd.ExecuteNonQuery()
 cmd.Connection.Close()
 cmd.Dispose()

It isn’t really that hard.  Just think of it as ordering a pizza.

  1. You want to order a pizza (add the name of your stored procedure)
  2. You get the phone number of the pizza parlor (connection string)
  3. You decide what toppings you want on it (parameters)
  4. Pick up the phone and wait for an answer (open connection)
  5. Place your order (execute query)
  6. hang up (close connection)

Now, there are a lot more things that the SQL Command can do – and better ways to do it, but this is a big step.  For our company development methodology, we start by using the RunSQL() command in the datalayer.  Once that’s done, we can move to using a SQLcommand which is sending the SQL statement directly from the page.  Once that’s done we move to this step, where we have moved the statment over to SQL Server as a stored procedure.  That’s the end.  Unless we really really really need some further efficiency or other problem, that’s the finished product as far as we will ever need it.

Article by MacGyver / ASP.net, Training

About MacGyver

I've worked with database systems for over 20 years, and started my own company in 2000. Almost all my business consists of internal database systems, either ERP or CRM. My programming is primarily in Angular / Microsoft C# and MS SQL.

About This Site

Morris Development has been specializing in internal database system design and integration since 1999. We provide long-term management and support of secure data systems for many businesses as well as developing the more complex code structures for ERP systems like Intellievent, Apidas, and AVMS.

This site is primarily for our developers to keep track up various technologies and updates that are used by Morris Development.

Training

Integrating Angular Microsite with .Net

Private Data Caching with Google Storage

Continuous Deployment for Production Releases?

Azure Websites – the perfect Angular host

Angular 2

  • Angular 2 Authentication
  • Angular Command Line Interface
  • Material Design for Angular
  • Using Observables in Angular 2

Mentors

  • Ben Nadel
  • Dan Wahlin
  • Deborah Kurata
  • John Papa

Staff

  • Dan Morris

Training

  • Google Development Courses
  • Microsoft Virtual Academy
  • PluralSight
  • Test Deep Links

© 2025 · Morris Development