SQL Server Connection String on Domain - sql-server

I have a local SQL Server DB on my domain-attached machine to which I'm trying to connect via a WPF app. The original connstring was:
Server=MyMachine.MyDomain.local;Database=MyDomain;Trusted_Connection=True
That resulted in a failed connection (could connect via SSMS just fine), access is denied. When I changed the connstring to:
Server=MyMachine;Database=MyDomain;Trusted_Connection=True
everything connected properly. Any thoughts as to why? It's not a big deal, as it's a test db, but I'd like to understand.

It's a guess, but I'd bet that name resolution is failing. Somehow the dns lookup is failing, but the short name works because it tries a netbios lookup instead of a dns lookup. It likely also works if you put in the server IP address.

I finally found the problem: protocol TCP/IP was disabled, so it appears that calling it via MyMachine.MyDomain.local was trying to use the TCP/IP protocol, even though it was on my local machine.

Related

Very slow SQL query when using IP in connection string

I am having a real big headache with slow SQL query. I have two connection string which are as follows :
ConnectString1 = "Driver={SQL Server};Server=MSSQLSERVER5;Database=SchoolMain;Uid=Admin;Pwd=admin101;"
and
ConnectString2 = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=Admin;Password=admin101;Initial Catalog=SchoolMain;Data Source=192.168.1.2,1433"
Both are connecting to a SQL Server database instance on the same system. The first connects using the database instance name, while second connects using IP address (over the internet to the system) and port number.
The query with ConnectString1 is very first, takes less than 2 seconds to execute while query using ConnectString2 is extremely very slow and most times comes back with timeout expired error.
I have searched everywhere on the internet and still cannot find where the issue is. I read about turning off the LLMNR protocol and adding entries in the hosts file to tackle Reverse DNS, followed the steps but it's still the same as query with ConnectString2 is still very slow.
Though when I changed the IP address in ConnectString2 from 192.168.1.2 to 127.0.0.1, the query works very fast, just exactly as it is with ConnectString1.
Is there a way to route all IP address to 127.0.0.1 on the machine?
I need ConnectString2 to work query will be pushed over the IP address from other systems outside the LAN.
Note: I am using SQL Server 2008
Please help.
The main reason this may happen is reverse DNS, meaning the sql needs to translate the IP to a physical address and it takes time.
To fix this problem use your host file to add the address to your machine and than use the name given at the host file instead of the IP.
If you do not want to temper with the host file try to look at the alternate solution to enable the TCP/IP which is disabled by default.

Azure Function Database Connection

I have a Python package that I am able to run successfully on an Azure Data Science Virtual Machine. However, when I push it to Azure as a Function, I cannot successfully make a database connection. I was getting an error that the ODBC Driver 13 for SQL Server was not supported, so I changed the driver to ODBC Driver 17 for SQL Server and now I am NOT getting an error, but no data is being returned for a query that I know should return data.
Is there any other reason that data would not be returned? Firewall issues? do I need to add a binding? Do I need to separate out the connection string to feed each part (e.g., Driver, UID, PWD) into pyodbc.connect() separately? Right now I am feeding it in like this:
setting = os.environ("CONNECTIONSTRING")
conn = pyodbc.connect(setting)
This query works fine returning data when I run it on the VM using this code, just not as a Function.
(Note, this is different from my previous post regarding reading the Azure App Setting. That problem has been solved).
There are many parts where this could be breaking.
I'd suggest start by having a Profiler or Extended Events trace on your SQL Server to verify whether a connection is even being established. If not then you need to work through the the various points of connectivity to find out where it breaks. The identity, firewall, NSGs etc might all come into play here.
Once you see a connection then you can play with permissions to ensure that your query then returns your data.
Without a full picture of your infrastructure and settings it is hard to pin it down further.
Turns out it was not a database connectivity issue like I thought it was; it was a code error.

How do I setup OLEDB to connect to a remote database?

I am locally running an Oracle 11g database. I have a small program connecting to it in code via OLEDB in VC++ (It only runs some database tests, I'm making sure I have all the basics down before I go into the real thing.) The connection information in code only includes the provider, instance name, user name, and password. All this aspect works fine.
//For example, both these ways of connecting work:
result = dataSource.Open(DATABASE_PROVIDER, DATABASE_NAME,
DATABASE_USER_NAME, DATABASE_USER_PASSWORD);
result = dataSource.OpenFromInitializationString(L"Provider=OraOLEDB.OracleDataSource=orcl;User ID=SYSTEM;Password=admin;");
I now want to send this program to other computers in my network and run it from there, connecting to my database on my local machine.
How would I go about connecting the other computers to my database in a way that the code will understand?
I have been trying to connect locally via IP instead of "localhost", figuring I could then simply use the same code and client. In that regard, I have tried a few things without success:
-I have tried modifying the connection string to change "Data Source" to my IP, but it could not connect.
-I have tried adding some parameters from other connection string examples I had seen, but they were not for Oracle and were ignored.
-I have also tried modifying tnsnames.ora and listener.ora to change local host to an IP address, but I know that didn't work, as it would still connect if I entered rubbish.
Anyone has the knowledge to help out?
For an Oracle db, you should set up a new tnsnames entry on their individual machines that points to your local db. Then use that new tns name as the datasource. You'll also need to make sure that your local db instance is accessible to them in the first place. (This is not enabled by default in Oracle Express, by the way.)
I've also generally had more success using msdaora as the data provider instead of OraOLEDB.

Do connection string DNS lookups get cached?

Suppose the following:
I have a database set up on database.mywebsite.com, which resolves to IP 111.111.1.1, running from a local DNS server on our network.
I have countless ASP, ASP.NET and WinForms applications that use a connection string utilising database.mywebsite.com as the server name, all running from the internal network.
Then the box running the database dies, and I switch over to a new box with an IP of 222.222.2.2.
So, I update the DNS for database.mywebsite.com to point to 222.222.2.2.
Will all the applications and computers running them have cached the old resolved IP address?
I'm assuming they will have.
Any suggestions along the lines of "don't have your IP change each time you switch box" are not too welcome as I cannot control this aspect of the situation, unfortunately. We are currently using the machine name of the box, which changes every time it dies and all apps etc. have to be updated with the new machine name. It hurts.
Even if the DNS is not cached local to the machine, it will likely be cached somewhere along the DNS chain between the machine and the name servers, at least for a short while. My understanding is this situation would usually be handled with IP takeover where you just make the new machine 111.111.1.1.
Probably a question for serverfault.
You're looking for DNS TTL (Time To Live) I guess.. In my opinion applications may cache the IP for at most the value of the TTL. I'm afraid however that some applications/technologies might actually cache it longer (agian in my opinion completely wrong)
Each machine will cache the ip address.
The length of time it is cached is the TTL (Time To Live). This is a setting on your DNS server, if you set it very low say 5 mins, then you show be up and running fairly quikly. A bit of a hack but it should work.
Yes, the other comments are correct in that what controls this is the DNS TTL set for the hostname database.mywebsite.com.
You'll have to decide what the maximum amount of time you're willing to wait for if you have a failure on your primary address (111.111.1.1) after you make the switch to the secondary address. Lower settings will give you a quicker recovery time, but will also increase the load and bandwidth to your DNS server because clients will have to re-query it to refresh their cache more often.
You can use nslookup using the -d option from your cmd prompt to see what your default TTL times and remaining TTL times are for the DNS server you are querying.
%> nslookup -d google.com
You should assume that they are cashed for two reasons not clearly mentioned before:
1- Many "modern" versions of OS families do DNS caching.
2- Many applications do DNS caching or have poor error/failure detection on live connections and/or opening new connections. This would possibly include your database client.
Also, this is probably not well documented. I did some googling, and found this for MySQL:
http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connecting-connection-string.html#connector-net-programming-connecting-errors
It does not clearly explain its behavior in this regard.
I had a similar issue with a web site that disables the application pool recycling features and runs for weeks on end. Sometimes, a clustered SQL Server box would restart and for some reason, my SqlConnection's were not reconnecting. I was getting the error:
A network-related or instance-specific
error occurred while establishing a
connection to SQL Server. The server
was not found or was not accessible.
Verify that the instance name is
correct and that SQL Server is
configured to allow remote
connections. (provider: Named Pipes
Provider, error: 40 - Could not open a
connection to SQL Server)
The server was there - and running - in fact, if I just recycled the app pool, the app would work fine - but I don't like recycling app pools!
The connections that were being held in the connection pool were somehow using old connection information, and that could have been old IP addresses. This is what seems so similar to the poster's question, that it appears to be cached DNS information, because as soon as some sort of a cache is cleared, the app works fine.
This is how I solved it - by forcing all of the connections in the pool to be re-created:
Try
' Example: SqlDependency, but this could also be any SqlConnection.Open call
Dim result As Boolean = SqlClient.SqlDependency.Start(ConnStr)
Catch sqlex As SqlClient.SqlException
SqlClient.SqlConnection.ClearAllPools()
End Try
The code sample is just the boiled-down basics - it should be tweaked for your situation!
The DNS gets cached, but for any server that resolves to the wrong ip address, you can update the HOSTS file of the server and the ip should be updated immediately. This could be a solution if you have a limited amount of servers accessing your database server.

BizTalk: In the remote connection dialog, is there any way to force TCP/IP transport rather than Named Pipes?

I am trying to connect to a biztalk instance over the network, and not being all that successful. The SQL server with the biztalk databases on it has named pipes disabled. When I try to connect to the server that has biztalk installed, it gives a named pipes error. When I try to connect to the database server, it just times out and gives an error that covers virtually every scenario under the sun (basically it can't connect and it doesn't know why)
If I try to connect with SQL Server Management Studio it works. If I try to make a dsn, it works. If I try to go through biztalk, it dies. If I try to go through biztalk with the tcp: prefix, it dies. If I force named pipes on the other two, it dies.
Is there any way from the Connect to existing group dialog to force using TCP/IP as the transport?
To force tcp , try "tcp:servername" (or "tcp:servername\instancename,port")
This can be done without setting an alias or default protocol with SQL tools
There are alternatives to why you may get the error though:
The named pipes error can be misleading, It usually just means "can't find server"
firewalls etc are blocking access to either the SQL instance
... or SQL listener port 1434 thats enumerates instance/ports
do you get any error messages in system or application log?
you may need to check some of the shapes - ports etc as there may be
http://www.biztalkserverguide.com/2008/02/04/biztalk-server-throwing-named-pipes-error-40-could-not-open-a-connection-to-sql-server/
declared a variable called btsConnString (string), this varable was not set to the connection string, it was blank ( by mistake), things were working as expected without issue. On the Stage As the btsConnString wasn’t assigned a connection string, it was throwing the error
Also look at MSDTC issues for connection DTC Test is here http://support.microsoft.com/kb/293799 and if you can install on both servers use DTCPing
also look at theseThe majority of Microsoft SQL Server issues that affect Microsoft BizTalk Server 2006 fall into one of the following categories:
I believe I had something similar fairly recently - BizTalk would try to connect to the SQL box using named pipes for some obscure reason, which would time out.
We've using the registry (HKLM\SOFTWARE\Microsoft\MSSQLServer\Client\SuperSocketNetLib) to force it to try TCP first (and last, and we haven't specified any other protocol) and it seemed to do the trick.
See details here
I know this is an extremely old thread but for reference with the exact same problem. I ended up creating an alias to the instance in SQL Server Configuration Manager. Your alias would look like this: Alias = Aliasname Value = Server\Instance. Then put Aliasname as your datasource in your connection string. Seems to me BizTalk doesn't understand the backslash (\) in the connection string. It must have the a single word.

Resources