using System.Data.SqlClient; using System.Data; public static class SqlErrorRepro { private static string sqlBatchText = @" BEGIN TRY SELECT name, log(max_length) FROM sys.columns END TRY BEGIN CATCH ; THROW END CATCH"; public static string connString = @"Data Source=.\SEIZE;Integrated Security=SSPI;Database=tempdb"; public static void DtLoad() { using (SqlConnection cn = new SqlConnection(connString)) using (SqlCommand cmd = new SqlCommand(sqlBatchText, cn)) { try { cn.Open(); DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); System.Console.WriteLine("{0} rows read", dt.Rows.Count); } catch (System.Exception ex){ System.Console.WriteLine("ERROR: " + ex.Message); } } } public static void DaFillDt() { using (SqlConnection cn = new SqlConnection(connString)) using (SqlCommand cmd = new SqlCommand(sqlBatchText, cn)) { try { cn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); System.Console.WriteLine("{0} rows read", dt.Rows.Count); } catch (System.Exception ex){ System.Console.WriteLine("ERROR: " + ex.Message); } } } public static void DaFillDs() { using (SqlConnection cn = new SqlConnection(connString)) using (SqlCommand cmd = new SqlCommand(sqlBatchText, cn)) { try { cn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); System.Console.WriteLine("{0} rows read", ds.Tables[0].Rows.Count); } catch (System.Exception ex){ System.Console.WriteLine("ERROR: " + ex.Message); } } } public static void Main() { System.Console.WriteLine("DataTable.load"); DtLoad(); System.Console.WriteLine("DataAdataper.Fill(DataTable)"); DaFillDt(); System.Console.WriteLine("DataAdataper.Fill(DataSet)"); DaFillDs(); } }