Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Writing SQL commands in C# - Part 1 - INSERT into table

sql command

  • Please log in to reply
9 replies to this topic

#1 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 29 August 2009 - 02:35 AM

Welcome all! Hereby I would like to present you a little prototype of mine. This is a program showing that you can indeed write SQL commands using C#. Try out the code below, and if you are interested in reading further tutorials... please leave me a comment and some +rep. ;)

Connecting or creating a database

For a start let's run Visual Studio 2008. I also installed SQL Express which runs locally. If you want to connect remotly using TCP/IP then we can work on that, too. But I do not do that in this tutorial, part 1.

sshot-2.jpg

Now, before touching any database you need to do three things. For one, you need to create a database. In the Database Explorer click-right and click Add Connection. There you select a database provider (SqlClient is default) and a database file. I selected a new database file, to create a new database. Go ahead and create your own database, I will wait.

sshot-5.jpg

For two, you need to create a table. Again in the Database Explorer select the database, then Tables folder, then right-click and click Add New Table. Now go ahead and put few columns there, along with their types. I used types int and ntext. Better avoid other types, like DateTime . Then save the table, pick your own name for it. Go ahead.

sshot-8.jpg

For three, you need to make a connection string. This time use the Data Sources panel and right-click, then click Add New Data Source. The wizard is pretty straightforward. Select a Database. It should propose you the database file, that you selected earlier.

Warning: It will ask you do you want to copy the database into your project. If you click yes then it will restore the database at every run. I strongly suggest to click No. Otherwise you will loose your records all too often.

Next page will ask to save a connection string. You are going to need it in the code. Last page will ask you to import tables. I suggest you just import them all and sort it out later.

sshot-15.jpg


Writing SQL command: INSERT into table

Here we are. The precious moment of truth. Let us insert a nice record into our new table. Here is a simple piece of code that does that. Notice that you are going to include an import directive, use two classes, and that is about it.

The namespace System.Data.SqlClient contains the two classes, SqlConnection and SqlCommand that will do the whole work. As for the SQL language itself, there is some nice MSDN documentation for that.

using System.Data.SqlClient;


SqlConnection connection1 = new SqlConnection(
Properties.Settings.Default.some_numbersConnectionString);

SqlCommand insertCommand = new SqlCommand(
"INSERT into NumbersTable values (128, 'as in 128 bits equal 16 bytes')",
connection1);

connection1.Open();
insertCommand.ExecuteNonQuery();
connection1.Close();

MessageBox.Show("Record inserted. Please check your table data. :)");


Here are some screenshots for the code, and running. Sorry, I could not resist to add some.

sshot-19.jpg


Looking at the actual database records

So how do you know that the INSERT command worked? Well, I do not mention how to pull out the records from a database, yet, thus you need to look up the database itself. Thankfully VS2008 has a nice feature. In the Database Explorer right-click the table and click Show Table Data. You see... all your inserts are okay. Nothing else to do, but to save the project.

[ATTACH]1988[/ATTACH]

All comments, critisism and +rep are welcomed. Go ahead and reply to me! ;)

Attached Thumbnails

  • sshot-21.jpg
  • sshot-18.jpg
  • sshot-17.jpg
  • sshot-13.jpg
  • sshot-14.jpg
  • sshot-12.jpg
  • sshot-11.jpg
  • sshot-10.jpg
  • sshot-6.jpg
  • sshot-3.jpg
  • sshot-1.jpg

Attached Files


  • 2

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 29 August 2009 - 07:46 AM

Wow, this was a well done tutorial! I love the use of the tables, highlighting and screenshots. Very nice indeed! Do you like using ADO.NET for databases? I found it a bit to complex and helpful where I just wanted to do most of it manually.

+rep
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 29 August 2009 - 08:05 AM

I really don't care for ADO (.NET or otherwise). The style just irritates me. +rep though :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 29 August 2009 - 08:39 PM

Awesome tutorial thanks Arek +rep
  • 1

#5 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 29 August 2009 - 08:40 PM

I would like to give you rep Arek but it says i must spread some rep around before giving it all to you Arek... that sucks. why?
  • 0

#6 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 31 August 2009 - 12:06 AM

You have to +rep few other people before repping me again. I am sure there are some people you would like to rep, too. ;)
  • 0

#7 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 01 September 2009 - 02:51 AM

You have to +rep few other people before repping me again. I am sure there are some people you would like to rep, too. ;)

Announcement: Part 2 tutorial is available. It covers reading several records, and deleting them.
  • 0

#8 SNK111

SNK111

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 31 May 2010 - 10:52 PM

This is a very useful article.

Here is another article I believe others will find useful too: HOW TO: SQL & C# .

Check the article for details on how to connect to SQL server database from C#.NET database applications as well as Java database applications. It also describes how to pass embedded SQL queries, calling stored procedures, pass parameter etc.
  • 0

#9 kemnet

kemnet

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 14 January 2011 - 01:10 PM

I did everything this post said...but my table doesnt get the "128" and as in 128 bits equal 16 bytes' thou it does show the messagebox and say my tables were updated
  • 0

#10 mvr210

mvr210

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Learning:C#

Posted 10 January 2013 - 09:16 AM

Fantastic! Exactlly what I was looking for.

Thank you, Miro

+rep

Edited by mvr210, 10 January 2013 - 09:25 AM.

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download