March 1, 2021
Hot Topics:

Updating a Database from WinForms Controls, Old School

  • By Paul Kimmel
  • Send Email »
  • More Articles »

Listing 1: Insert a row in the Northwind Categories table from the TextBox values.

Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles Button1.Click

   Dim connectionString As String = _
      "Data Source=CASPAR\SQLEXPRESS;Initial " + _
      "Catalog=northwind;Integrated Security=True"

   Using connection = New SqlConnection(connectionString)

      Dim insert As String = String.Format( _
         "INSERT INTO Categories (CategoryName, Description ) " + _
         "VALUES('{0}','{1}')", TextBox1.Text, TextBox2.Text)
      Dim command As SqlCommand = New SqlCommand(insert, connection)

   End Using

End Sub
End Class

Here is some "how-to" information you will want to know.

Add the Imports statement at the top of the listing by typing. The actual assemblies (fancy .NET speak for DLLs) for database stuff are already referenced in WinForms projects. (You can click the Show All Files button—shown in Figure 4—to see the references to included DLLs.)

Figure 4: Click Show All Files to see all of the legwork Visual Studio has done for you, including adding references to useful assemblies.

The connection string is easy to get, too. Click the Northwind database in the Server Explorer and copy the connection string from the Properties window (see Figure 5). The rest of the code is boilerplate. If you don't want to remember how to write this boilerplate code, you can select it all—after its in Visual Studio one time) and drag and drop in the Toolbox. Then, like everything else in the toolbox, you can drag from the toolbox to your project at any future time.

Figure 5: The connection string can be copied from the Properties window.

Run the project, add some text values, and you are off to the races. You can browse the database table to check your results.


Older techniques, such as updating a SQL database with inline SQL (like the example in this article), still work. Sometimes, it seems like technology changes exist to sell more software or to torture us. Even though software companies have to sell product to stay in business and sometimes new technologies feel like torture, the general belief is that changes in the way we do things exist to make us more productive.

After 20 plus years of experience, I can tell you that if you are just learning how to update a database in the manner shown, you are fortunate. The reason you are fortunate is that you can tuck this tidbit away and then skip this how-to approach and move on to LINQ. Focusing your energies on LINQ (besides being cooler) will make you immediately more productive and it's a lot more fun than cobbling inline SQL together.

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his book LINQ Unleashed for C#, now available on Amazon.com and at fine bookstores everywhere. Look for his upcoming book Teach Yourself the ADO.NET Entity Framework in 24 Hours. You may contact him for technology questions at pkimmel@softconcepts.com. Paul Kimmel is a Technical Evangelist for Developer Express, Inc.

Copyright © 2008 by Paul Kimmel. All Rights Reserved.

Page 3 of 3

This article was originally published on February 12, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date