c# - Can open a connection to a non-existing database, how is this possible? -


i have desktop software. @ login test if database can reached opening connection. if can't throws exception. if can good, close connection , go main window. until here normal. in mainwindow have thread checks connection 1 1 minute calling isdbconnectionok method below:

sqlconnstring =  @"data source=(localdb)\v11.0;attachdbfilename=" + configurationmanager.appsettings["databasepath"] + ";integrated security=true"; sqlconn = new sqlconnection(sqlconnstring);    private bool isdbconnectionok(string errormessage) {     using (var l_oconnection = new sqlconnection(sqlconn.connectionstring))     {         try         {             l_oconnection.open();             return true;         }         catch (sqlexception)         {             if (!string.isnullorempty(errormessage))             {                 this.showmessageasync("waring", errormessage);             }             return false;         }     } } 

i test inserting usb stick database , connecting it. when i'm in mainwindow after first check unplug stick , wait thread check again. connection opened without problem altough there no database connect to. can please explain this? how in world cacheing or happening? how come login part throws error if database path invalid or cannot reached , in main window keeps being able open connection when no database present?

try :

change d:\sample.mdf file path

 private string _connectionstring =         @"data source =(localdb)\v11.0;attachdbfilename=d:\sample.mdf;" +         "integrated security=true";      private bool isdbconnectionok(out string errormessage)     {         using (var connection = new sqlconnection(_connectionstring))         {             try             {                 connection.open();                 if (connection.state != connectionstate.open)                 {                     errormessage = null;                     return false;                 }                 errormessage = null;                 return true;             }             catch (exception exp)             {                 errormessage = exp.message;                 return false;             }                         {                 connection.close();             }         }     }      private void btntestconnection_onclick(object sender, routedeventargs e)     {         string errormessage;         var isok = isdbconnectionok(out errormessage);         if (isok)         {             messagebox.show("connection ok");         }         else         {             messagebox.show("connection error : " + errormessage);         }     } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -