using System;
using System.Data;
using System.Data.SqlClient;
class SqlExceptionDemo {
static void Main(){
string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "something wrong here";
try {
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
string str = dr.GetValue(20).ToString();
dr.Close();
}
catch (System.InvalidOperationException ex)
{
string str;
str = "Source:" + ex.Source;
str += "
" + "Message:"+ ex.Message;
str += "
" + "
";
str += "
" + "Stack Trace :" + ex.StackTrace;
Console.WriteLine (str, "Specific Exception");
}
catch (System.Data.SqlClient.SqlException ex)
{
string str;
str = "Source:" + ex.Source;
str += "
" + "Message:" + ex.Message;
Console.WriteLine (str, "Database Exception");
}
catch (System.Exception ex)
{
string str;
str = "Source:" + ex.Source;
str += "
"+ "Message:" + ex.Message;
Console.WriteLine (str, "Generic Exception");
}
finally
{
if ( conn.State == ConnectionState.Open)
{
Console.WriteLine ("Finally block closing the connection", "Finally");
conn.Close();
}
}
}
}
Month: February 2011
SqlException message
using System;
using System.Data;
using System.Data.SqlClient;
class SqlExceptionDemo {
static void Main(){
string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "wrong sql";
try{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Close();
} catch (System.Data.SqlClient.SqlException ex) {
string str;
str = "Source:" + ex.Source;
str += "
" + "Exception Message:" + ex.Message;
Console.WriteLine(str);
}
catch (System.Exception ex)
{
string str;
str = "Source:" + ex.Source;
str += "
" + "Exception Message:" + ex.Message;
Console.WriteLine(str);
}
finally
{
if (conn.State == ConnectionState.Open)
{
Console.WriteLine("Finally block closing the connection", "Finally");
conn.Close();
}
}
}
}
GetDataSources
using System;
using System.Data;
using System.Data.Sql;
class MainClass {
public static void Main(string[] args) {
using (DataTable SqlSources = SqlDataSourceEnumerator.Instance.GetDataSources()) {
Console.WriteLine("Discover SQL Server Instances:");
foreach (DataRow source in SqlSources.Rows) {
Console.WriteLine(" Server Name:{0}", source["ServerName"]);
Console.WriteLine(" Instance Name:{0}", source["InstanceName"]);
Console.WriteLine(" Is Clustered:{0}", source["IsClustered"]);
Console.WriteLine(" Version:{0}", source["Version"]);
}
}
}
}
SqlDataReader Ordinal Indexer
using System;
using System.Data;
using System.Data.SqlClient;
class OrdinalIndexer
{
static void Main(string[] args)
{
string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql = @"select FirstName, LastName from Employee";
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
Console.WriteLine(" {0} {1}", "First Name".PadRight(25),"Last Name".PadRight(20));
Console.WriteLine(" {0} {1}", "============".PadRight(25), "============".PadRight(20));
while(reader.Read()) {
Console.WriteLine(" {0} | {1}",
reader[0].ToString().PadLeft(25),
reader[1].ToString().PadLeft(20));
}
reader.Close();
} catch(Exception e) {
Console.WriteLine("Error Occurred: " + e);
} finally {
conn.Close();
}
}
}
Deal with Multiple Results
using System;
using System.Data;
using System.Data.SqlClient;
class MultipleResults {
static void Main(string[] args) {
string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql1 = @"select FirstName from Employee;";
string sql2 = @"select FirstName, LastName from Employee;";
string sql = sql1 + sql2;
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
do{
while(reader.Read()) {
Console.WriteLine("{0} : {1}", reader[0], reader[0]);
}
Console.WriteLine("".PadLeft(60, '='));
} while(reader.NextResult());
reader.Close();
} catch(Exception e) {
Console.WriteLine("Error Occurred: " + e);
} finally {
conn.Close();
}
}
}
Use While loop to read query result data from SqlDataReader
using System;
using System.Data;
using System.Data.SqlClient;
class DataLooper{
static void Main(string[] args) {
string connString = "server=(local)SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
string sql = @"select FirstName 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}", reader[0]);
}
reader.Close();
}
catch(Exception e)
{
Console.WriteLine("Error Occurred: " + e);
}
finally
{
conn.Close();
}
}
}
Use A Data Reader
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
using System.Data;
using System.Data.SqlClient;
namespace Client.Chapter_13___ADO.NET
{
public class UsingADataReader
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");
MyConnection.Open();
SqlCommand MyCommand = new SqlCommand("SELECT * FROM CaseInfo", MyConnection);
SqlDataReader MyDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (MyDataReader.Read())
{
Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);
}
MyConnection.Close();
}
}
}

