Get result data from SqlDataReader by type: decimal, string, int and boolean : Result Set « Database ADO.net « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Database ADO.net » Result SetScreenshots 
Get result data from SqlDataReader by type: decimal, string, int and boolean

using System;
using System.Data;
using System.Data.SqlClient;

   class TypedMethods
   {
      static void Main(string[] args)
      {
         string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
         string sql = @"select * from employee";
         SqlConnection conn = new SqlConnection(connString);

         try {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataReader reader = cmd.ExecuteReader();

            while(reader.Read()) {
               Console.WriteLine"{0}\t {1}\t\t {2}\t {3}"
                  // nvarchar
                  reader.GetString(0).PadRight(30),
                  // money
                  reader.GetDecimal(1),
                  // smallint
                  reader.GetInt16(2),
                  // bit
                  reader.GetBoolean(3));
            }
            reader.Close();
         catch(Exception e) {
            Console.WriteLine("Error Occurred: " + e);
         finally {
            conn.Close();
         }
      }
   }



           
       
Related examples in the same category
1. Result set info: column Name, column Type, type name
2. Result set info: field count and field type
w__w_w___.___j_ava___2___s.__c__o__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.