using System;
using System.Data;
using System.Data.SqlClient;
namespace SQLExample3
{
class Program
{
static string cs = "Data
Source=ssrap7;Initial Catalog=Test;Integrated Security=True";
static void Main(string[] args)
{
//using
statement to ensure dispose of
using (var conn = new SqlConnection(cs))
{
try
{
string GetPersons = "SELECT * from Person;";
var cmnd = new SqlCommand(GetPersons, conn);
conn.Open();
using (SqlDataReader dr =
cmnd.ExecuteReader())
{
try
{
if (dr.HasRows)
while (dr.Read())
Console.WriteLine("{0}) {1} {2} born {3}", dr[0], dr[1], dr[2], dr[3]);
}
finally
{
dr.Close();
}
}
}
catch (SqlException sqlEx)
{
//display sql exception
Console.WriteLine(sqlEx.Message);
}
catch (Exception Ex)
{
//display all other exceptions
Console.WriteLine(Ex.Message);
}
finally
{
//ensure
that the connection is closed everytime
conn.Close();
}
}
}
}
}
One thing to keep in mind is that you can use the data reader to return multiple result sets by appending your select queries and calling nextResult() to move to the next result set.
using System;
using System.Data;
using System.Data.SqlClient;
namespace SQLExample3
{
class Program
{
static string cs = "Data
Source=ssrap7;Initial Catalog=Test;Integrated Security=True";
static void Main(string[] args)
{
//using
statement to ensure dispose of
using (var conn = new SqlConnection(cs))
{
try
{
//Two select Statments Combined
string GetPersons = @"SELECT * from Person;
SELECT * from Person;";
var cmnd = new SqlCommand(GetPersons, conn);
conn.Open();
using (SqlDataReader dr =
cmnd.ExecuteReader())
{
try
{
do
{
if (dr.HasRows)
while (dr.Read())
Console.WriteLine("{0}) {1} {2} born {3}",
dr[0], dr[1], dr[2], dr.GetDateTime(3).ToShortDateString());
Console.WriteLine("\n***Next Result Set***\n");
} while (dr.NextResult());
}
finally
{
dr.Close();
}
}
}
catch (SqlException sqlEx)
{
//display sql exception
Console.WriteLine(sqlEx.Message);
}
catch (Exception Ex)
{
//display all other exceptions
Console.WriteLine(Ex.Message);
}
finally
{
//ensure
that the connection is closed everytime
conn.Close();
}
}
}
}
}
Now the sqlCommand class has a special function called ExecuteScaler() it is used when you need to retieve just one piece of data, this data comes back as an object so you should unbox it before you use it.
using System;
using System.Data;
using System.Data.SqlClient;
namespace SQLExample4
{
class Program
{
static string cs = "Data
Source=ssrap7;Initial Catalog=Test;Integrated Security=True";
static void Main(string[] args)
{
//using
statement to ensure dispose of
using (var conn = new SqlConnection(cs))
{
try
{
string GetPersons = "SELECT COUNT(id) FROM Person;";
var cmnd = new SqlCommand(GetPersons, conn);
conn.Open();
var retunrnValue =
cmnd.ExecuteScalar();
if (retunrnValue is Int32)
Console.WriteLine("There is {0} Person rows", retunrnValue);
}
catch (SqlException sqlEx)
{
//display sql exception
Console.WriteLine(sqlEx.Message);
}
catch (Exception Ex)
{
//display all other exceptions
Console.WriteLine(Ex.Message);
}
finally
{
//ensure that the connection is closed everytime
conn.Close();
}
}
}
}
}
No comments:
Post a Comment