Go Back   CodeCall Programming Forum > Software Development > Tutorials > CSharp Tutorials
Register Blogs Search Today's Posts Mark Forums Read

CSharp Tutorials Tutorials for C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-29-2009, 06:35 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Arrow Writing SQL commands in C# - Part 1 - INSERT into table

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.

writing-sql-commands-c-part-1-insert-into-table-sshot-1.jpgwriting-sql-commands-c-part-1-insert-into-table-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.

writing-sql-commands-c-part-1-insert-into-table-sshot-3.jpgwriting-sql-commands-c-part-1-insert-into-table-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.

writing-sql-commands-c-part-1-insert-into-table-sshot-6.jpgwriting-sql-commands-c-part-1-insert-into-table-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.

writing-sql-commands-c-part-1-insert-into-table-sshot-10.jpgwriting-sql-commands-c-part-1-insert-into-table-sshot-11.jpgwriting-sql-commands-c-part-1-insert-into-table-sshot-12.jpgwriting-sql-commands-c-part-1-insert-into-table-sshot-13.jpgwriting-sql-commands-c-part-1-insert-into-table-sshot-14.jpgwriting-sql-commands-c-part-1-insert-into-table-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.

PHP Code:
using System.Data.SqlClient
PHP Code:
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.

writing-sql-commands-c-part-1-insert-into-table-sshot-17.jpgwriting-sql-commands-c-part-1-insert-into-table-sshot-18.jpgwriting-sql-commands-c-part-1-insert-into-table-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.

writing-sql-commands-c-part-1-insert-into-table-sshot-21.jpg

All comments, critisism and +rep are welcomed. Go ahead and reply to me!
Attached Files
File Type: zip Sql command INSERT solution.zip (23.4 KB, 145 views)
File Type: zip some_numbers database.zip (154.7 KB, 96 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-29-2009, 11:46 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Writing SQL commands in C# - Part 1 - INSERT into table

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-29-2009, 12:05 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Writing SQL commands in C# - Part 1 - INSERT into table

I really don't care for ADO (.NET or otherwise). The style just irritates me. +rep though
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-30-2009, 12:39 AM
Siten0308's Avatar
Programming Professional
 
Join Date: Jun 2008
Location: California, USA
Posts: 283
Siten0308 will become famous soon enough
Red face Re: Writing SQL commands in C# - Part 1 - INSERT into table

Awesome tutorial thanks Arek +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-30-2009, 12:40 AM
Siten0308's Avatar
Programming Professional
 
Join Date: Jun 2008
Location: California, USA
Posts: 283
Siten0308 will become famous soon enough
Re: Writing SQL commands in C# - Part 1 - INSERT into table

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-31-2009, 04:06 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: Writing SQL commands in C# - Part 1 - INSERT into table

You have to +rep few other people before repping me again. I am sure there are some people you would like to rep, too.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-01-2009, 06:51 AM
ArekBulski's Avatar
Guru
 
Join Date: Mar 2009
Posts: 1,373
ArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really niceArekBulski is just really nice
Re: Writing SQL commands in C# - Part 1 - INSERT into table

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clipboard Game Jordan Games 3471 Today 12:14 AM
Building a Big Project, Part 3 WingedPanther PHP Tutorials 5 09-13-2009 06:49 PM
SQL joins chili5 Database & Database Programming 4 05-18-2009 10:55 AM
Tutorial: Advanced SQL Jordan Tutorials 2 04-07-2008 04:18 PM
Sudoku can be solved using SQL..Take a look! roger Database & Database Programming 5 07-04-2006 03:20 PM


All times are GMT -5. The time now is 06:51 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0