Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Retrieves data as native SQL Server types

using System; using System.Data.SqlClient; using System.Data.SqlTypes; public class NativeSqlServer {     public static void Main()      {         string SQL = "SELECT OrderID, CustomerID, OrderDate, Freight FROM Orders";         SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI");         SqlCommand cmd = new SqlCommand(SQL, con);         SqlDataReader r;         con.Open();         r = cmd.ExecuteReader();         while (r.Read()) {             SqlInt32 orderID = r.GetSqlInt32(0);             SqlString customerID = r.GetSqlString(1);             SqlDateTime orderDate = r.GetSqlDateTime(2);             SqlMoney freight = r.GetSqlMoney(3);             Console.Write(orderID.ToString() + ", ");             Console.Write(customerID + ", ");             Console.Write(orderDate.ToString() + ", ");             Console.Write(freight.ToString() + ", ");             Console.WriteLine();         }     }     }