Connection String for SQL Server Express - c#

I am using this connection string for SQL Server Professional. It is ok...
<add name="bd1" connectionString="Provider=SQLOLEDB.1;
Data Source=10.180.0.2;
User ID=sa;
Password=1234;
Initial Catalog=aa" />
But when I change for SQL Server Express 2008. Doesnt work.
String connection
<add name="bd1" connectionString="Provider=SQLOLEDB.1;
Data Source=10.180.0.8;
User ID=sa;
Password=1234;
Initial Catalog=aa" />
I have this error
[DBNETLIB][ConnectionOpen (Invalid Instance()).]Invalid connection.

By default, SQL Server Express installs as a "named instance", as \SQLEXPRESS. Technically, any SQL server instance can be installed as a "named instance" (under any name), and SQL Server Express can be installed under a different name, or as the default instance - these are just the installer defaults, but: they are common defaults.
To connect to a "named instance" (rather than the default instance), the instance name must be included in the connection string - i.e.
Provider=SQLOLEDB.1;Data Source=10.180.0.8\SQLEXPRESS;User ID=sa;...etc

Related

I can't connect to a local SQL Server instance via Entity Framework using Windows authentication

I can't connect to my local SQL Server Express instance via Entity Framework. When I try to run the update-database command, I get this error message.
Login failed for user ''. Reason: An attempt to login using SQL authentication failed.
Server is configured for Integrated authentication only.
Error: 18456, Severity: 14, State: 58.
From what I understand, Visual Studio is attempting to log in to SQL Server via a user account even though I've requested that Windows authentication is used in the connection string.
I can still access the server via SSMS.
What I've tried. None of which helped
Different variations of the connection string
Opened port 1433
Created a test UDL file to test the connection.
A clean reinstall of SQL Server Express (not sure I managed to clean up all the files)
Checked the SQL Server browser is running
Checked server instance is running
Enabled TCP/IP & named pipes
Tried connecting via tcp which works
Added Integrated Security=SSPI to connection string
Changed server to accept Windows authentication and SQL Server authentication.
Restarting PC
This is a new laptop on windows 11, I don't know if that is causing any issues as I've never had an issue with this process on Windows 10.
I seem to have two instances; .\SQLEXPRESS & (localdb)\\MSSQLLocalDB I'm not sure if this is causing some conflict or if this is the intended behaviour.
Here is the connection string, I pulled this from the server explorer within Visual Studio so I am pretty sure this is correct. On top of that, I have tried numerous variations of this to attempt to fix the issue
"DefaultConnection": "Data Source=LAPTOP-51LB4QTQ\\SQLEXPRESS;Initial Catalog=MicroBlog;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
This is where I get the connection string in my Program.cs file
builder.Services.AddDbContext<BlogContext>(opt => opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
At this point I'm completely lost, I've read a lot of articles but have not come across any fixes. I'm not a dba just a programmer so have limited knowledge of this side of SQL Server.
You may try like below:
In Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=SUBRATATALUKDER;Initial Catalog=MyDB;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
In appsettings.json file:
"ConnectionStrings": {
"DefaultConnection": "Server=SUBRATATALUKDER;Database=MyDB;Trusted_Connection=True;TrustServerCertificate=True;",
}
Note:
Server Name = SUBRATATALUKDER
Database Name = MyDB
100% tested.

How to make ConnectionString work on client computer?

I created a setup for a management system in Visual Studio and I used a Microsoft SQL Server database file, but when I used it on the user's computer or on another computer, I get this message when I tried to connect to the database. What is the problem?
I used SQL Server 2017 Express.
My connection string to work on client's PC :
#"Data Source=.\SQLEXPRESS01;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|gym.mdf";
I installed SQL Server 2017 Express on the client machine, but it didn't work.
What is the problem ?
By default, when you don't change any settings during installation, a SQL Server Express instance will get the SQLEXPRESS instance name - so try this:
Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|gym.mdf
(not SQLEXPRESS01 - that's not standard)
The error you get means that the server is not correct.
First, you can try the following code to get installed ServerName and instanceName.
var instances = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow instance in instances.AsEnumerable())
{
Console.WriteLine(instance["ServerName"]);
Console.WriteLine(instance["InstanceName"]);
}
Second, you can use the following connectionstring to connect db file.
string connstr = #"Data Source=server\\instance;
AttachDbFilename=D:\Product.mdf;
Integrated Security=True;
Connect Timeout=2;";

Entity Framework Connection to localDb

I am trying to follow the pluralsight course ASP.NET MVC 4 Fundamentals. But can't have my database connected.
Here is the error I got:
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
I have visual studio 2013 professional and SQL server 2012 installed on my machine. When I installed my SQL server, I created a server name "ABC" on my computer.
I have also installed sql localdb 11.0 separately, but it seems VS can't find the localDb connection. When I check Server Explorer -> add Connection, under server name list, only "ABC" is shown up.
Here is the connection string.
I also tried to use "Data Source = ABC; ...." it doesn't work either.
Update
Here is my connection string
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-eManager.Web-20141223223418;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-eManager.Web-2014122322341‌​8.mdf" providerName="System.Data.SqlClient" />
You could try this
In server explorer, right click, Choose Add Connection
enter (localdb)\v11.0 in as the server name
Choose your database and press connect
Right click properties on your new connection
Use that connection in string in your default connection
I.e.
<add name="DefaultConnection" connectionString="<Paste-connection-string-here>" providerName="System.Data.SqlClient" />
If that doesn't work, lets try starting it from the command line
Open command prompt
Run SqlLocalDB.exe start v11.0
Follow original steps , use the named pipe as your server name
If that doesn't work, lets try and connect via named pipes
Open command prompt
Run SqlLocalDB.exe info v11.0
Copy the Instance pipe name that starts with np:...
Follow original steps , use the named pipe as your server name
e.g
Run this command to make sure what is the version of your LocalDB
sqllocaldb info
So in my case the version is MSSQLLocalDB then the connection string will look like this
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=IdentityManagerDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
To add on to TheGeneral use view - SQL Server Object Viewer and look immediately under the "SQL Server" object. You'll see the name of the connect looking something like "(localDB)\ProjectsV13" Hand enter that into the connection box then you can browse the server for the database you want to use.

C# Linq-to-sql server name when creating a release

I'm working on C# linq-to-sql application over SQL Server express 2008 . My database is inside SQL server.
I'm using Windows Authentication to connect to sql server from my application.
The server name used in connection string is related to my pc user name, so I'm wondering if I create a release of my application and put it on client pc which have SQL Server installed with another server name, would my application work?
my connection string is
<add name="WindowsFormsApplication1.Properties.Settings.OPTICA_MDFConnectionString"
connectionString="Data Source=ENG-MEDIAN-PC;Initial Catalog=OPTICA.MDF;Integrated Security=True"
providerName="System.Data.SqlClient" />
Because the app and the sql server are on the same host you can use "Data Source=.\<instance name>" which may be the default MSSQL.
That default would look like this:
connectionString="Data Source=.\MSSQL; ... "
For another example, our developer boxes' app.config uses this:
connectionString=".\DEVSQL;Initial Catalog=..."
Note that the "." is the host and that "DEVSQL" is the sql server instance name.

Exception when connecting to database on client machine

I have created a WPF application which used entity framework and SQL server 2012 for database. It runs fine on my machine. But when I ran this on client machine I received exception. This happens when I am trying to add a record in database.
Exception: The underlying provider failed on Open
Inner Exception: 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: SQL Network Interfaces, ........
Connection string:
<connectionStrings><add name="SchoolModelContext"
connectionString="metadata=res://*/SchoolModel.csdl|res://*/SchoolModel.ssdl|res://*/SchoolModel.msl;
provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;
initial catalog="SCHOOLDB.MDF"
integrated security=True;MultipleActiveResultSets=True;
App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
The error message ... it gives hints that are sometimes rabbit-trails. Basically the error is saying "I can't connect to sql Server, there is something wrong".
But.. you say you "installed SqlLocalDB" on the client.
Did you "wire up" the new database (SCHOOLDB)?
http://msdn.microsoft.com/en-us/library/hh212961.aspx
A. Creating an Instance of LocalDB
The following example creates an instance of SQL Server Express LocalDB named SCHOOLDB using the SQL Server 2012 binaries and starts the instance.
SqlLocalDB.exe create "SCHOOLDB" 11.0 -s
Aka, did you run something like that?
(Other info from the MSDN article that may not be relevant to this question)
B. Working with a Shared Instance of LocalDB
Open a command prompt using Administrator privileges.
SqlLocalDB.exe create "SCHOOLDB"
SqlLocalDB.exe share "SCHOOLDB" "SchoolSharedLocalDB"
SqlLocalDB.exe start "SCHOOLDB"
SqlLocalDB.exe info "SCHOOLDB"
REM The previous statement outputs the Instance pipe name for the next step
sqlcmd –S np:\\.\pipe\SCHOOLDB#<use your pipe name>\tsql\query
CREATE LOGIN NewLogin WITH PASSWORD = 'Passw0rd!!#52';
GO
CREATE USER NewLogin;
GO
EXIT
Execute the following code to connect to the shared instance of LocalDB using the NewLogin login.
sqlcmd –S (localdb)\.\SchoolSharedLocalDB -U NewLogin -P Passw0rd!!#52
EDIT ::: (only read above this line for a general understanding...see below on steps I actually did)
Ok, the above is a little confusing because its hard to know if you're dealing with the ~instance or the database.
Let's try this:
SqlLocalDB.exe create "MySuperCoolLocalExpressInstance"
SqlLocalDB.exe share "MySuperCoolLocalExpressInstance" "MySuperCoolLocalExpressInstanceSHARED"
SqlLocalDB.exe start "MySuperCoolLocalExpressInstance"
SqlLocalDB.exe info "MySuperCoolLocalExpressInstance"
NOW, very important, get YOUR very unique "named pipe" value.
np:\\.\pipe\LocalDB#<use your pipe name>\tsql\query
Copy your SchoolDB.mdf and SchoolDB.ldf to :
c:\MyCoolLocalDatabaseFiles\
Now set your connection string to this:
<connectionStrings>
<add name="SchoolModelContext"
connectionString="metadata=res://*/SchoolModel.csdl|res://*/SchoolModel.ssdl|res://*/SchoolModel.msl;
provider=System.Data.SqlClient;provider connection string="data source=np:\\.\pipe\LOCALDB#MYUNIQUEPIPENAME\tsql\query;attachdbfilename=C:\MyCoolLocalDatabaseFiles\SchoolDB.mdf;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
But substitute "MYUNIQUEPIPENAME" for your value.
The key is "attachdbfilename". This is how to "on the fly" attach a database.
You have some alternates for using the named pipe syntax. See:
http://www.connectionstrings.com/sql-server-2012
"User Instance on local SQL Server Express"
enter code here

Categories

Resources