Handle and diagnose database connection exceptions
"server=.;database=northwind;integrated security=true;connection timeout=30"try
{
var cnSetting = ConfigurationManager.ConnectionStrings["nw"];
using (var cn = new SqlConnection())
using (var cmd = cn.CreateCommand())
{
cn.ConnectionString = cnSetting.ConnectionString;
cmd.CommandTimeout = 60;
cmd.CommandText = "Select @@version";
cn.Open();
MessageBox.Show(cmd.ExecuteScalar().ToString());
}
}
catch (SqlException ex)
{
MessageBox.Show("SQL Exception: " + ex.Message);
}PreviousConnect to a data source by using a generic data access interfaceNextManage exceptions when selecting, modifying data
Last updated