Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL server alias recognized with CLR runtime but not CoreCLR #14945

Closed
ketrex2 opened this issue Jul 31, 2015 · 12 comments
Closed

SQL server alias recognized with CLR runtime but not CoreCLR #14945

ketrex2 opened this issue Jul 31, 2015 · 12 comments
Assignees
Milestone

Comments

@ketrex2
Copy link

ketrex2 commented Jul 31, 2015

I wrote a test console application and set up a SQL configuration alias in SQL server configuration manager. When running my application with the dnx 4.51 runtime, the database connection is established with no problem and the application functions normally

If I switch the runtime to dnx core 5.0, any attempts to connect to the database from the application fail with the SqlException 'The network path was not found.' Replacing the alias in the connection string with the actual server name resolves the issue. The connection string I'm using is included below:

"connectionString": "Server=aliasName;Database=dbName;Trusted_Connection=True;MultipleActiveResultSets=true"

Perhaps the System.Data.SqlClient coreCLR library does not support SQL aliases and this is not a bug. I just think the different behavior between runtimes is a bit odd and the error it produces is not helpful in deciphering the problem.

@divega
Copy link
Contributor

divega commented Jul 31, 2015

FYI @YoungGah

@joshfree
Copy link
Member

joshfree commented Oct 5, 2015

@saurabh500 can you follow up please

@YoungGah
Copy link

YoungGah commented Oct 5, 2015

@Phasiq The support for SQL alias has been intentionally removed from CoreFX.

@saurabh500
Copy link
Contributor

Closing issue as aliases are not supported in CoreFX

@CumpsD
Copy link

CumpsD commented Sep 28, 2016

Is there a plan/workaround to support them?

A library somewhere, a snippet to make it work, a roadmap?

@robdmoore
Copy link

Why aren't aliases supported? I find this to be a really useful feature since often people will have (local) or .. or localhost as the server in the connection string, but I prefer to install SQL Express (quicker/easier/smaller install) and then use aliases so I can work with whatever connection string is in source control in the various projects I work on. I'm a consultant so often work in different codebases and don't always have control over the connection string used by the teams I work with.

@saurabh500
Copy link
Contributor

The SQL server alias is defined on each client machine and it points to a SQL server instance. The alias information is stored in the registry on Windows. This is a OS specific dependency which can work only on Windows. As a result we decided to drop support for sql alias in core fx.

@robdmoore
Copy link

robdmoore commented Mar 10, 2017 via email

@droyad
Copy link

droyad commented May 5, 2017

This workaround does the trick (WOMM)

var builder = new SqlConnectionStringBuilder(config.ConnectionString);

var key = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "x86"
    ? @"HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\MSSQLServer\Client\ConnectTo"
    : @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client\ConnectTo";

var newSource = (string)Microsoft.Win32.Registry.GetValue(key, builder.DataSource, null);
if (newSource != null)
    builder.DataSource = newSource.Substring(newSource.IndexOf(',') + 1);

config.ConnectionString = builder.ConnectionString;

@droyad
Copy link

droyad commented Sep 22, 2017

I've pulled the above code into a lib and NuGet Package https://github.com/droyad/SqlAlias

@gerardog
Copy link

People do use Sql Aliases. Solving this issue in corefx will kill the need to manually fix it on each .Net Core application. Examples are dbcli/mssql-cli#100 and microsoft/azuredatastudio#71

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 1.0.0-rc1 milestone Jan 31, 2020
@ststeiger
Copy link

How about


            if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
            {
                string key = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") == "x86"
                ? @"HKEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\MSSQLServer\Client\ConnectTo"
                : @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\MSSQLServer\Client\ConnectTo";

                string newSource = (string)Microsoft.Win32.Registry.GetValue(key, csb.DataSource, null);
                if (newSource != null)
                    csb.DataSource = newSource.Substring(newSource.IndexOf(',') + 1);
            }

@dotnet dotnet locked as resolved and limited conversation to collaborators Jan 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests