Forum Discussion

jthompson1's avatar
jthompson1
Contributor
9 years ago

Simple ADO Connect to SQL 2012

I want to connect to a localhost SQLEXPRESS 2012 Database through the use of a script. I have spent far too long trying to accomplish what I would figure to be a simple task. I actually got the connectionstring from TestComplete when I used the wizard to do a data store. TestComplete had no issues connecting via the data wizard. I can also connect to my database using MSSMS, Visual Studio, Toad, web.config... you name it... I cannot connect through a test script.

 

I get the error : [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied

 

I've looked at countless articles written by the smartbear team, and am still having issues here. I could use some assistance with this one. Thank you.

 

 

function CheckDatabase()
{

var ADOConnection;

 ADOConnection = ADO.CreateADOConnection();

 ADOConnection.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=noneofyourbusiness;Initial Catalog=master;Data Source=.\sqlexpress"

ADOConnection.CommandTimeout = 60;
ADOConnection.LoginPrompt = false;

try
{
ADOConnection.Open();
Log.Message("Success!!")
}
catch(e)
{
Log.Message(e.message)
}
}

4 Replies

  • I connect to SQL a lot from script. Works fine.

     

    Your connection string looks the same as mine, apart form the last parameter. I use the computer name. Which I pull from an environment variable at runtime. In C# Script:

     

     var OWS = Sys["OleObject"]("WScript.Shell");
     var CompName = OWS.ExpandEnvironmentStrings("%COMPUTERNAME%")

     

     

    My final parameter in the connection string is

     

    + ";Data Source=" + CompName;

     

     

     

    Mine have always been on the local machine thus far, but I believe you need the IP or hostname of the remote machine (and it needs to be configured to allow remote connections to the DB I think).

    • tonydugay's avatar
      tonydugay
      Contributor

      I agree with Colin - I connect to a remote DB and my connection string looks the same - but I use an ip address at the end of the string.

  • Well I do appreciate the replies. Makes me feel a little more confident that I am headed in the right direction, however I still have not resolved this issue. Should I find a solution I will be sure to reply on this thread. :smileyvery-happy:

    • NisHera's avatar
      NisHera
      Valued Contributor

      I do like this and works for me....

       

       

          Connect = ADO.CreateConnection();
      ADOCommand = ADO.CreateCommand();
      ConnectionString = 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=';
      ConnectionString = ConnectionString + 'DatabaseName';
      ConnectionString = ConnectionString + ';Data Source=' + 'SQLServerName';

      Connect .ConnectionString = ConnectionString ;
      Connect .Open();
      ADOCommand.ActiveConnection = Connect ;
      ADOCommand.CommandType = adCmdText;
      ADOCommand.CommandText = "My...SQLQuery";
      RecordSet = sADOCommand.Execute();

       I think what you are missing is

       ADOCommand = ADO.CreateADOCommand();