How to connect to SQL Server using Windows Auth? - sql-server

I am currently using this example to connect to SQL Server using Go:
Create Go apps
Here is the example I am using:
package main
import (
_ "github.com/denisenkom/go-mssqldb"
"database/sql"
"context"
"log"
"fmt"
)
// Replace with your own connection parameters
var server = "localhost"
var port = 1433
var user = "sa"
var password = "your_password"
var db *sql.DB
func main() {
var err error
// Create connection string
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d",
server, user, password, port)
// Create connection pool
db, err = sql.Open("sqlserver", connString)
if err != nil {
log.Fatal("Error creating connection pool: " + err.Error())
}
log.Printf("Connected!\n")
// Close the database connection pool after program executes
defer db.Close()
SelectVersion()
}
Is there any known way to use Windows Authentication to connect to SQL Server? I have tried adding "Trusted_Connection=yes" and removing the username/password.
I have Googled around but have not found any Go Packages that have this option.

As I posted earlier...
"trusted_connection=yes" did work. I just entered it wrong.

In my workplace we are using GORM which uses go-mssqldb to connect to the SQL Server. The following connection string format worked for us:
sqlserver://DATABASE_HOSTNAME:1433?database=DATABASE_NAME&trusted+connection=yes&app+name=APPLICATION_NAME

Your client side setup was correct. Did you add the windows user account to to the Database? I have done this multiple time without issues. Make sure your windows auth is enabled in your Db and the user has sufficient previlages.
steps to connect to sql server using windows authenticaton

Related

Cannot connect to On-Premises SQL Server Express Instance with go-mssqldb

I have ported a ETL utility I wrote over from C# to Go that performs a reporting task. The C# app is able to connect to the SQL Express server just fine but the Go app is struggling to find the named instance of SQLEXPRESS.
I have used both versions:
server=<server>\<instance>;user id=<user>;password=<passw>;database=<database>;
sqlserver://<user>:<passw>#<server>/<instance>?database=<database>
...in various iterations when executing:
db, err := sql.Open("sqlserver", connString)
if err != nil {
panic(err)
}
... and in both cases I get the the following error back:
no instance matching 'SQLEXPRESS' returned from host 'localhost'

"Integrated Security not supported" in google apps script jdbc connection

I'm trying to connect to my MS-SQL DB with Google Apps Script using the JDBC function.
We use windows integrated security, but when i try to connect with this parameter i get an error saying that it's not supported.
I've already tried to provide my windows credentials and using the param "IntegratedSecurity=true" but can't connect, tried with server IP and Name but doesn't work either.
var address = 'serverIP'; //ex. '10.1.1.1:1433'
var user = ';username=username';
var userPwd = ';password=password';
var db = 'dbname';
var dbUrl = 'jdbc:sqlserver://' + address + ';databaseName=' + db;
function readData() {
var conn = Jdbc.getConnection(dbUrl,user,userPwd);
}
I've tried this form too:
var conn = Jdbc.getConnection("jdbc:sqlserver://MyServerIP:1433;databaseName=DBName;integratedSecurity=true");
It should connect to the server but all I get is errors
The errors are
In the first form:
"Unable to connect to a database. Check the connection string,
username and password. (line 12, file "Code")"
And in the second form:
"The following connection properties are not
supported: integratedSecurity. (line 13, file "Code")"
The instructions for using Windows Integrated Authentication from non-Windows platforms is here: Using Kerberos integrated authentication to connect to SQL Server On Windows you have an additional option using a platform .dll as described here: Connecting with integrated authentication On Windows
I have no idea whether either of these is viable in whatever "Google Apps Script" is.

windows authentication on SQL server

I am trying to connect to a SQL server using Windows authentication. I am doing it through node js on Visual Studio.
The SQL server is on my machine itself.
It had worked fine for the connection string
var config = {
server: '10.2.9.208',
database: 'DFMProAnalyticsCopy',
user: 'sa',
password: 'admin123#',
port: 1433 //default port number
};
But this was only for SQL authentication.
Now for Windows aunthentication, I have this connection string
var config =
"Driver ={SQL Server};Server = 10.2.9.208;Database=DFMProAnalyticsCopy;Trusted_Connection=True;";
This gives me error
Unknown driver SQL Server!
and this connection string
var config =
"Server = 10.2.9.208;Database=DFMProAnalyticsCopy;Trusted_Connection=True;";
gives error
Login failed for user ''
It is trying to attempt SQL authentication when I want Windows authentication. I have Mixed mode authentication ON.
I read at many places but am still unclear about it.
"Login failed for user ''" when trying to use Windows Authentication
Please help me.
EDIT 1:
I cannot use the configuration string as in SQL Server connection with jQuery and node js
because mine is centralised and anyone could be using it, so I need to use Trusted_connection=true or Integrated security = SSPI. I dont have windows login credentials of people using the system.
This worked for me. I had to provide an UID and password in the connection string, else it considered a null string for UID
var config = "Server={IPV4 of server};Database=dbname;Uid=domain\\username;Pwd=pwd";

Delphi 7 failing to pickup username and password from UDL

I'm having issues trying to get Delphi 7 to connect to a remote MSSQL server.
This is the extent of the code:
UDLPath := 'FILE NAME=C:\Path\To\UDL.UDL';
TestConnection := TADOConnection.Create(nil);
with TestConnection do
begin
ConnectionString := UDLPath;
try
Connected := True;
except
ShowMessage('Failed');
end;
end;
I get the error "Login failed for user ''. The user is not associated with a trusted SQL Server connection"
I have LoginPrompt set to false in the IDE. I tried setting it to true, but it's hitting the except before it prompts for a login.
The test connection button works in the UDL, and I can connect to the server through SSMS.
I've tried using a raw connection string with the userid and password fields set properly, but that didn't work.
maybe this could help you, see the two links:
https://support.microsoft.com/en-us/kb/889615
http://blog.sqlauthority.com/2014/01/17/sql-server-fix-login-failed-for-user-username-the-user-is-not-associated-with-a-trusted-sql-server-connection-microsoft-sql-server-error-18452/
The issue was with the provider. The native client 10 driver was failing to authenticate for an unknown reason, but the ODBC for SQL driver worked fine.

Unable to connect to MS SQL Server using Go

I am trying to connect to sql server express using go and this sql driver "github.com/denisenkom/go-mssqldb"
Currently it calls open then stalls on the ping command, it never pings or returns an error.
I am using winauth and it works with sql management studio.
I have verified the database name, if I put a false one in, an error is generated and logged at the open step.
I have tried setting a really short connection time out but the same thing happens.
I have tried connecting to a remote sql server and it works correctly.
The remote server is a developer version of sql server and had a name like xyz.abc.123 where my local sql express is called machine-name\sqlexpress.
Here is a sample program.
package main
import (
"database/sql"
_ "github.com/denisenkom/go-mssqldb"
"log"
)
func main() {
log.Println("Main:")
log.Println("Opening")
db, err := sql.Open("mssql", "server=Machine-Name\\SQLEXPRESS; database=MyDatabaseName;")
if err != nil {
log.Println("Open Failed: ", err.Error())
}
log.Println("Opened")
log.Println("Pinging")
err = db.Ping();
if err != nil {
log.Println("Failed to ping: ", err.Error())
}
log.Println("Pinged")
}
I encountered this same issue not too long ago and after two days of researching I was able to fix the issue.
The confusing aspect seems to come from the fact that the go-mssqldb needs a parameter passed named "server" and in SQL Management Studio they have a clear reference to server in the connection properties:
Connection Properties
However, this is not the value go-mssqldb is looking for. It is looking for the actual server where the DB is installed. Meaning, if it is installed in your local PC this would be "localhost". If it's on another machine in your network, then this would be the corresponding IP address or server name
Another thing to keep in mind is that go-mssqldb needs a port to connect to the DB and if you don't provide one, it assumes the port is 1433. However, mine wasn't that value. In order to look for your port number:
Open up SQL Server Configuration Manager.
Expand "SQL Server Network Configuration" on the tree on the left.
Click on "Protocols for SQLEXPRESS (or whatever name your DB has)"
In the main menu, make sure "TCP/IP" is enabled. If not, right-click on it and select "Enable".
Right-click on the "TCP/IP" again and select properties.
Select the "IP Addresses" tab and scroll all the way down.
Look at the section: "IPAll" and the number displayed in the "TCP Dynamic Ports" field is the correct port number.
SQL Server Configuration Manager
Finally, the command I ended up using to run my code ended up being:
sqlserver-agent -debug=true -server=localhost -port=62587 -user=Username -password=Password -database=DatabaseName
This https://code.google.com/p/odbc/ driver works with MS SQL Server for me.
Alex

Resources