Unable to connect to SQL instance with SMO via C#


i having situation occur quite odd.  have instance of sql 2008 r2 adventureworks db attached.  if try connect database sqlconnection object, works expected.  need use smo , connection failing when using same connection string.  , mean same - using same const value within same method.  below code, along notation of fails , error message is:

public void testsqlconnection() { const string constring = @"integrated security=sspi;data source=maindev\sqlexpress;persist security info=false;initial catalog=adventureworks2008r2"; const string databasename = "adventureworks2008r2"; // first try connect sqlconnection

sqlconnection conn = new sqlconnection(constring); try { conn.open(); system.diagnostics.debug.write(string.format("via sqlconnection: {0}", conn.state)); } catch (exception ex) { showexception(ex); } { conn.close(); } // try using smo objects serverconnection connection = new serverconnection(constring); server serverinstance = new server(connection); try { // try out database object - error occurs database db = new database(serverinstance, databasename); } catch (exception ex) { // showexception method helper displays exception message , recurses down // inner exception showing each message until there aren't more showexception(ex); /* * initial exception message: setparent failed database 'adventureworks2008r2'. * inner exception (1): failed connect server integrated security=sspi;data source=maindev\sqlexpress;persist security info=false;initial catalog=adventureworks2008r2. * inner exception (2): network-related or instance-specific error occurred while establishing connection sql server. server not found or not accessible. * verify instance name correct , sql server configured allow remote connections. (provider: sql network interfaces, * error: 26 - error locating server/instance specified) */ } }


the serverconnection constructor not take same connection string sqlconnection takes. serverconnection takes instance name. try:

serverconnection connection = new serverconnection("maindev\sqlexpress");

alternatively can create sqlconnection instance so:

sqlconnection cxn = new sqlconnection(constring); serverconnection serverconnection = new serverconnection(cxn);




Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'