SQL Server 2000 / ODBC - Not associated with a trusted SQL Server connection - sql-server-2000

I get this error:
ERROR [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user '(null)'. Reason: Not associated with a trusted SQL
Server connection.
Here's my code (C#), I'm running this on a local and on a remote machine:
using (OdbcConnection connection = new OdbcConnection("dsn=mydsn"))
{
connection.ConnectionTimeout = 50000;
OdbcCommand command = new OdbcCommand("select * from users", connection);
command.CommandTimeout = 50000;
connection.Open();
OdbcDataReader reader = command.ExecuteReader();
}
DSN details:
name: mydsn
SQL login: mylogin
database: Test
SQL Server Configuration:
security: sql server and windows authentication mode
Whenever I try to use use the login to Management Studio, everything works fine, I can execute queries.

Check the SQL client network utility and list the possible client protocols.
At least two should be enabled (by default):
TCP/IP
Named Pipes
I got rid of this error as soon as I've put TCP/IP first, named pipes second after switching to Windows Authentication only.
However, you may notice, that "trusted SQL Server Connection" usually refers to Windows Authentication only. If I were you, I would check the connection via osql first with your login and pass.

ODBC ALWAYS uses a trusted connection so the user account (on Windows) must have required privileges to access the database.

Related

SQL Server 2008 Express and windows authentication

I have a SQL Server 2008 Express instance running and I can access the server with the default account sqlexpress using sqlcmd.
Now I have created a new windows account on the machine where SQL Server is running and created a login for sqlexpress using windows authentication. This account would be specifically used for backups.
When I try to login using the following and run the backup script (this backup script works fine for the default account sqlexpress)
sqlcmd -S (local)\mywinusr -i TestBkup.sql
I get the error
HResult 0xFFFFFFFF, Level 16, State 1
SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired. tting login failed for the user.
What could be the problem?
sqlcmd -S specifies a server, you have specified (local)\mywinusr. this suggests that you are passing in a username as an instance. your server name for sqlexpress should be:
(local)\sqlexpress
if you are logged inas this new user, then you just specify -E for 'trusted connection'
if you need to connect as, use
-U domain\username and -P password
hope this helps

Unable to use vbscript to connect to sql server on different domain

I'm using vbscript to try to connect to a sql server on another domain. The server is part of a cluster and is SQL Server Data Center Edition (64bit). I can ping the server from my machine. I'm using the IP address to connect which works fine on SSMS.
The vbscript code I'm using is:
Set cn = CreateObject("ADODB.Connection")
cn.Open "DRIVER={SQL Server};Server=123.456.345.567\instance;Database=MyDB;User
ID=domain\myuserid;Password=mypass;"
strCommandText = "Select * from mytable"
cn.Execute strCommandText
When I run this, I get the following error:
Microsoft OLE DB Provider for
ODBC Drivers: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'domain\myuserid'.
I can't join my test machine to the domain and I can't use sql authentication as it's disabled on the sql server.
What do I need to do to get this to work?
Looking at you query string I see you are specifying username and password, and that means you are providing SQL Authentication credentials. However in your question you state SQL Authentication is disabled in the SQL server.
That's the reason why your SQL server is not letting you in. You are providing SQL authentication credentials when your server is not expecting them.
If SQL Authentication is disabled, then is very likely your server is not in Mixed Mode Authentication either so that pretty much means the SQL server is using Windows Authentication for logins. This configuration is beyond your control, it's your DBA who made that choice.
Therefore, if the SQL server you are connecting to is using Windows Authentication mode, your query string should be:
cn.Open "DRIVER={SQL Server};Server=123.456.345.567\instance;Database=MyDB;Trusted_Connection=yes;"
However here comes the tricky part... if you are running your VBScript from your test machine that is part of [DOMAIN_A], it will run with under a [DOMAIN_A] windows credentials. However your SQL server is expecting windows credentials from the domain he is joined into. Let's call it [Domain_B].
Unless you AD admins have created trust relationship between [DOMAIN_A] and [DOMAIN_B] your login attempts will still fail, despite now having a correct query string.
How do you solve this cross-domain issue if you can´t join your test machine to [DOMAIN_B]? Easy. Use the "Runas" command.
On your test machine joined to [DOMAIN_A], open a command prompt and execute:
Runas /noprofile /netonly user:[DOMAIN_B]\myuserid %comspec%
It will ask you for the password. Enter the "mypass" password you originally had in your query string. "myuserid" is also the username in your original query string.
After doing that, you will have a new command prompt window, but if you notice in its title, it is now running under your [DOMAIN_B] credentials.
Despite still being on your test machine joined to [DOMAIN_A], anything that you execute on this new command prompt will do it under your [DOMAIN_B] credentials. Exactly what your SQL server is expecting.
So at this point, only thing left is to run your VBScript on this new command prompt window. That should provide the appropriate credentials to SQL server.
Let us know if that solved the issue.
To my knowledge, there isn't an option to connect without joining the domain or using SQL Authentication. You might have luck connecting through a VPN in which you virtually join a domain.
Try changing your connection string, specifically driver to provider:
Give a try this one
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;
Here are conenction string examples:
http://www.connectionstrings.com/sql-server-2005#sql-server-native-client-10-0-oledb-provider

Cannot connect to sql server 2008 using JDBC

I have tried all the methods to connect to the server.I am using sql server 2008 on mixed authentication mode.I can login via the sql server management studio using the sa login.But when I try accessing the same db from my java program I get the following error:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:191d94bb-a55e-47a0-bb9f-b84638313cf4
This is my connectionstring:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl= "jdbc:sqlserver://localhost;databaseName=Northwind;user=sa;password=sa;";
I have accessed my database in the same way. One of my problems were that I hadn't enabled the port and IP for the database. Check these options in SQL Server Configuration Manager. Hope this helps =)

Debugging vb.net execution of database function call to remote sql server

Working in VWD 2010 Express
I have an aspx.vb file with the following connection string:
Protected Function getPasswordLength() As Integer
Dim conn As New SqlConnection("Data Source=localhost;database=MyDB;Integrated Security=true")
Dim cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = "select dbo.getPWLen() ;"
conn.Open()
getPasswordLength = CInt(cmd.ExecuteScalar())
conn.Close()
End Function
This works when the code is on the web server / which is collocated with the SQLServer.
However, when I want to debug, I have to copy to the server. I would like to be able to "debug in place" on my development machine and only copy files to the production machine periodically. Technically the production machine is not in production at the moment, but that's beside the point.
Anyway, I followed the following instruction: http://weblogs.asp.net/nannettethacker/archive/2008/02/17/creating-a-database-connection-to-a-remote-sql-server-database-within-visual-web-developer.aspx
I can now see the remote database, functions, etc. on the other machine (from the VWD running on the development machine).
However, I thought I should then be able to execute this same code locally on the development machine using ctrl-F5, but I get the following error:
--
**Server Error in '/' Application.
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)**
Should what I'm doing be possible? If so, any ideas what I'm doing wrong?
Make sure your SQL Server is configured to accept remote connections.

Why can I connect to sql server 2008 via odbc but not through vb.net code?

I'm supporting an old vb.net program whose database it connected to was moved from SQL Server 2005 to SQL Server 2008. Is there a setting on SQL Server 2008 which will allow ODBC connections to access the database but not allow VB.NET to connect to it programmatically?
the error i keep receiving in the app is:
An error has occurred while
establishing a connection to the
server. When connecting to SQL Server
2005, this failure may be caused by
the fact that under the default
settings SQL Server does not allow
remote connections. (provider: Named
Pipes Provider, error: 40 – Could not
open a connection to SQL Server)
however I can connect to it when I create a system dsn to the sql server instance and through VS2005's Tools >> Connect to Database.
Here is the code I'm using to connect:
dim strC as string
strC = "data source=bob; database=subscribers; user id=bobuser; password=passme"
dim connection as New SqlClient.SqlConnection(strC)
try
connection.open()
catch ex as Exception
msgbox(ex.message)
end try
connection.Close()
There is no such setting but it is possible because: You are able to access your sever via ODBC because using an ODBC DSN creates a sort of local bridge to the server. Trying to connect to the server from VB.NET code with the SqlClient library from a client machine requires your Sql Server to accept remote connections...
Check this MSDN article for help.
In brief:
You need to set Sql Server to allow remote connections
You need to make sure your firewall isn't blocking Sql Server's port(s)
Make sure TCP/IP is enabled as a comm. mechanism for Sql Server
Start > Run > "CLICONFG" (No quotes)
In the SQL Server Client Network Utility, enable Named Pipes over TCP/IP