0

Im trying to use concatenation to make a connection string. Heres the code:

objMyConn.ConnectionString = "Provider=SQLOLEDB;Initial Catalog=MyCat;Data Source=" & serverName & ";User ID=MyId;Password=MyPwd;"

serverName is a string defined like this: serverName = "ServerName\InstaceName"

I am expecting for objMyConn.Connection string to be = to the whole connection staring however it is = to this:

Provider=SQLOLEDB;Initial Catalog=MyCat;Data Source=ServerName\InstanceName

So I'm missing the last part of my connection string.

7
  • Can you try serverName = "ServerName\\InstanceName" ? Commented Nov 25, 2015 at 21:48
  • 1
    The \ char needing to be escaped was my first thought also. however this did not solve the problem. Commented Nov 25, 2015 at 21:52
  • What happens if you set serverName = "fred"? Do you still lose the last part of the conn string? Commented Nov 25, 2015 at 21:55
  • Possible duplicate of ADODB Connection.ConnectionString property doesn't return server name Commented Nov 25, 2015 at 22:04
  • 1
    According to MSDN: ADO Connection String, you should use single quote mark ' in your connection string, so objMyConn.ConnectionString = "Provider='SQLOLEDB';Initial Catalog='MyCat';Data Source='" & serverName & "';User ID='MyId';Password='MyPwd';" Commented Nov 25, 2015 at 22:04

1 Answer 1

0

Here is the solution to this, Just in case anyone else has the same problem.

When declaring my variable server name I had given it a length of 255, I changed the length to the appropriate length needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.