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-31-2009, 05:40 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 2 - Reading and deleting records

Welcome all! This is a second part of my SQL in C# tutorial series. If you are interested in writing INSERT commands then take a look at part 1: INSERT into table. In this tutorial I would like to show you how to read several records by using a SELECT statement, and how to delete records by using DELETE statement. Enjoy this short tutorial.

Reading records with SQL SELECT statment: using SqlDataReader class

So, so far we created a database, with a table for numbers and descriptions, and added a record. Remember, table NumbersTable has two columns, numbers and description. During insertion and deletion column names are not used, you need only their order. So when you write some code, take a look again at the table. If you make it wrong then god help you with debbuging.

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

SqlCommand insertCommand = new SqlCommand(
    "INSERT into NumbersTable values (1002, 'will delete this one')",
    connection1);

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

MessageBox.Show("Record inserted. Please check your table data. :)");
But a database is useless if you cannot use the data you inputed there, right? After all this is why created a database in the first place. So, let us read all the records from our table. While you can use some conditions to select only several records, it is not necessary in this case.

For a start you need a SqlConnection and SqlCommand classes. They are required for any SQL statement. This time however, we could also use a SqlDataReader class. It fetches the received data and lets you access them one record at a time.

After calling reader = selectCommand.ExecuteReader(), you are able to access your records one-by-one. By turns, call reader.Read() while checking are there any more records to read. If it returns false then you are done with reading. Then process the current record by reading reader[0], reader[1] and so on. Again, field order is important. Try hard to avoid making mistakes here.

Code:
SqlConnection connection1 = new SqlConnection(
    Properties.Settings.Default.some_numbersConnectionString);
SqlCommand selectCommand = new SqlCommand(
    "SELECT * FROM NumbersTable",
    connection1);

connection1.Open();
SqlDataReader reader = selectCommand.ExecuteReader();
string fetchedRecords = string.Empty;

while (reader.Read() == true)
{
    fetchedRecords += "Record: (number) " + reader[0] + " (descriptio) " + reader[1] + " \n";
}

connection1.Close();
MessageBox.Show("Found following records: \n \n" + fetchedRecords);


Deleting several records: SQL DELETE statement

Well, deletion is as easy as a pie. There is nothing more than the short code below. Note that you can use several conditions, but ntext and varchar field types do not play with equal-to operator (=). That is why the other version is commented out.

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

SqlCommand insertCommand = new SqlCommand(
    "DELETE NumbersTable WHERE number = 1002",
    //"DELETE NumbersTable WHERE number = 1002 AND description = 'will delete this one'",
    connection1);

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

MessageBox.Show("Record 1002 deleted. Please check your table data again. :)");


Thank the author, share some reputation points

I would like to thank everyone, especially Jordan and Siten, that were supporting me. Writing tutorials is a hard work. If you want to leave a comment or +rep my post then go ahead. I thank you in advance.
Attached Thumbnails
writing-sql-commands-c-part-2-reading-deleting-records-sshot-4.jpg   writing-sql-commands-c-part-2-reading-deleting-records-sshot-6.jpg  
Attached Files
File Type: zip SqlCommand example, version 02, Insert Select Delete.zip (55.9 KB, 43 views)
File Type: zip some_numbers database.zip (157.5 KB, 37 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-31-2009, 02:37 PM
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 2 - Reading and deleting records

Wow, very nicely done! I enjoy reading your tutorials and will continue to support you. +rep!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-31-2009, 04:26 PM
gaylo565's Avatar
Programming Professional
 
Join Date: May 2007
Location: flagstaff, az
Posts: 251
gaylo565 is a jewel in the roughgaylo565 is a jewel in the roughgaylo565 is a jewel in the rough
Re: Writing SQL commands in C# - Part 2 - Reading and deleting records

I like the both of your recent SQL tut's. Very nicely put together. Should be helpfull to many a learning programmer. +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-31-2009, 08:09 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 2 - Reading and deleting records

+rep
__________________
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
  #5 (permalink)  
Old 09-01-2009, 06:32 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 2 - Reading and deleting records

Thank you all three. I appreciate your comments a lot. Thanks to you I achieved a Guru status, too. *hurray*
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
arek bulski, c# code, sql delete, sql select



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

Advanced Search
Display Modes



All times are GMT -5. The time now is 08:21 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0