I have coded my own tiny static DAL class for .NET Compact Framework and local SQL Server that does just what I need; GetTable(SQL,params), Exec(SQL,params), ExecScalar(SQL,Params) etc. I wanted to do everything by SQL so that's how I rolled, if there are better ways to handle SQL Connection in C# -except ORM- please let me know. Here's how I give public access to SQLConnection object under my DAL Class:
public static SqlConnection Conn()
{
if (ConnStr == "")
Defaults();
try
{
if (conn == null)
conn = new SqlConnection(ConnStr);
if (conn.State != ConnectionState.Open)
{
conn.Open();
Execute("SET DATEFORMAT DMY",null);
}
return conn;
}
catch
{
System.Windows.Forms.MessageBox.Show("Unable to connect to DB");
}
return null;
}
The application I'm coding is being used on mobile Windows CE 5.0. The problem is, when user gets disconnected from wireless network then reconnects, if (conn.State != ConnectionState.Open)
line doesn't get executed and application gives a general database exception, nothing else. How do I fix this?
Edit: Turns out exception is raised by functions such as GetTable(). I can handle it using try-catch in each function, but unfortunately in Compact Framework every sql error is named as "SqlException". How do I differ connection problems and query problems?