Published: 31 Aug 2011
By: Chris Siermacheski

This article will provide you with the small amount of code required to insert, update and retrieve values with up to date methods in Visual Studio.

Contents [hide]

Intro

I have found many different ways on the internet and in books on how to accomplish the simple tasks of inserting, updating and retrieving values in a SQL database. Unfortunately most of the articles, posts, forums etc... that talk about this seem to give long drawn out instructions that sometimes work and sometimes do not.

Don't get me wrong, many of the resources I found were getting the task done but most of them wanted you to code the connection to the database all in C#. This is a step that is not necessary, this article will provide you with the small amount of code required to insert, update and retrieve values with up to date methods in Visual Studio.

First off is Inserting

  1. Drop a SQLDataSource control onto the page
  2. Configure the data source (connect it to the database, select columns etc...)
  3. All you need for the code behind is the following code (no extra imports needed)

Code

Listing 1: Inserting

Second is Updating/Selecting

  1. I combined the update and select examples together in a simple hit counter, and like I stated before by select I mean retrieving a value from the database using C#. In this example I retrieve a value and assign it to a variable. This hit counter, like I said above, is very simple, it will add another visitor even if they just refresh the page.
  2. Drop and configure SQLDataSource control onto the page
  3. Put the following in the code behind

Code

Listing 2: Updating/Selecting

Summary

That's all there is to it, this is an easy way to insert, update and retrieve values in a Microsoft SQL database using C# in Visual Studio. Feel free to use the above code, fix it up, or customize it, whatever you would like.

I hope that this helps.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Chris Siermacheski

www.electricalbuyandsell.com

View complete profile here.

Other articles in this category


Lucene.NET vs SQL Server Full-text – Generating a million records and a full-text index
In this article we will take a look at how SQL Server performs with one million records in a table. ...
Creating Pivot Table and Pivot Chart with data from SQL Server 2008
This article shows how you may visualize data on your SQL Server 2008 with Pivot tables and Pivot Ch...
Identifying currently running SQL queries
This article is taken from the book SQL Server DMVs in Action. The author discusses a routine that i...
What's blocking my running SQL?
This article is taken from the book SQL Server DMVs in Action. The author discusses a routine that i...
SQL Azure to Developers: Part 1
In this part we will focus on overview of SQL Azure along with a first look on SQL Azure Management ...

You might also be interested in the following related blog posts


ASP.NET Data Control Events read more
Did you know? There are many ways to insert a snippet into your code (Lisa Feigenbaum) read more
Announcing the Web Platform Installer Release Candidate! read more
OUTPUTing Data from the Just-Inserted, Updated, or Deleted Row(s) read more
LINQ to SQL Dynamic Mapping read more
Free Download: VS 2008 Database Edition Power Tools read more
SqlNetFramework an easier alternative to microsoft data application block read more
Building Interactive User Interfaces with Microsoft ASP.NET AJAX: Using the UpdatePanel read more
How to debug a stored procedure in your Sql Server 2005 read more
The SQL Data Tools in Orcas read more
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.