I am trying to connect to a locally installed instance of SQL Server 2012 Express using VB.NET. I have been able to successfully connect to the database using the drag-and-drop connection tool, which gives me this connection string:
Data Source=mycomputername\sqlexpress;Initial Catalog="my space containing database name";Integrated Security=True
I can successfully test this connection. Now, I want to populate a datagrid from a table in this database, so in the "root" (what do you really call this place?) of the project I have:
Imports System.Data
Imports System.Data.SqlClient
and in the form definition (before any event handlers) I have
Private cn As New SqlConnection("Data Source=mycomputername\sqlexpress;Initial Catalog=[my space containing database name];Integrated Security=True;")
Private da As New SqlDataAdapter("select * from MyTable", cn)
Private ds As New DataSet
Private cmb As New SqlCommandBuilder(da)
And this is where the train goes off the tracks. I get the error:
Cannot open database "MyDatabase" requested by the login.
The login failed. Login failed for user 'MyLogin'.
I have made sure that SQL Server Express has the Remote Login check box checked, and tried other variations on the connection string, but no luck. Ideas much appreciated.
Are you using ASP.NET? It's possible that your application runs under a different user than your visual studio (Integrated Security uses your windows authentication, based on the user your application is impersonating - in a web application, that's usually someone like NETWORK SERVICE). You'd just have to add this user as one of the users of your SQL server and database.
EDIT: I see it now. '\s' is a special character in a string.
use this as your connection string:
"Data Source=mycomputername\\sqlexpress;Initial Catalog=[my space containing database name];Integrated Security=True;"
EDIT: No, my bad, you're using VB, this kind of escaping is used in C#.
can anyone tell me how to change from windows authentication to SQL server authentication.I have been using this as my connection string
"server=.;Data Source=.;Initial Catalog=Work;Integrated Security=True"
Everything You Always Wanted to Know About Connection Strings But Were Afraid to Ask - www.connectionstrings.com
The following connectionString format should do the job.
connectionString="user id=yourUsername; password=yourPassword;data source=yourServerName;initial catalog=yourDatabaseName;"
I have a website project (C#/ASP.NET) opened in Visual Studio 11 (beta) which works with the built-in SQL Server Compact Edition. But, I would rather use my SQL Server 2012 which is installed on this machine, and I have my tables already created in it.
Question is, how do connect to it from VS11? Do I add it in the App_Data folder where I have the Compact database?
Right now I am opening my pre-made database with the
var db = Database.Open("StarterSite");
command.
If you already have the database created on SQL Server 2012, then just use it!
No need to add it to your project (and most definitely don't copy it into App_Data!).
Just create a connection string in your web.config something like:
<connectionStrings>
<add name="YourConnectionStringNameHere"
connectionString="server=(local);database=YourDatabase;Integrated Security=SSPI;" />
</connectionStrings>
and then use that in your app using all the normal ADO.NET components like SqlConnection or SqlCommand:
string connectionString = WebConfigurationManager.ConnectionStrings["YourConnectionStringNameHere"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("your-sql-statement-here", conn))
{
// do work in your database here....
}
Or use something like Entity Framework to make your life even easier!
It will depend on the technique you wish to use to connect to the SQL server database. I prefer to set my connection string in the web.config and use that when connecting to my database. There are several options you might want to explore. Here is a good MSDN article explaining some techniques.
Connecting to Databases in ASP.NET
I need to connect to a SQL Server DB running on my laptop from another computer on a network. I am doing this both from a C# app as well as in a VBA macro. Both run perfectly on my laptop and the C# app runs perfectly over the network. However I cannot connect using VBA over the network. This is my connection string:
ConnectionString = "Driver={SQL Server};Server=MY-LAPTOP; DAtabase=SAFEXlive; UID = MyUsername; PWD=MyPassword"
Aside from the 'Driver={SQL Server}' this is the same as the connection string I am using in the C# app which works.
I am then using the following code (With a reference to Microsoft ActiveX Data Objects 6.0 Library in VBE) to open the connection:
Dim oConnection As Connection
Set oConnection = New Connection
oConnection.ConnectionString = strConnectionString
oConnection.Open
This works correctly if I run it on my laptop but if I run it on another computer on the network I get the error: "Run-time error '-2147217843 (80040e4d) [Microsoft][ODBC Server Driver][SQL Server]Login failed for user..." and the user it specifies it the windows log in for the computer.
Are there some security settings I need to set in code or in excel? Or am I constructing the connection string incorrectly? What am I doing wrong?
Solved. The answer is rather infuriating. The problem is in fact with the connection string, with the UID. Believe it or not changing ...UID= MyUsername;... to ..UID=MyUsername;..., i.e. removing the space character, was all it took! Thanks for suggestions though.
Try this Connection string,
ConnectionString = "Provider=SQLOLEDB;Data Source=MY-LAPTOP;Initial Catalog=SAFEXlive;User ID=MyUsername;Password=MyPassword"
Is this an AD Domain login? Make sure you have appended the domain to the username e.g, domain\user .
I suggest using integrated security instead of this.
I have two apps that use Integrated Security. One assigns Integrated Security = true in the connection string, and the other sets Integrated Security = SSPI.
What is the difference between SSPI and true in the context of Integrated Security?
According to Microsoft they are the same thing.
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
Integrated Security=true; doesn't work in all SQL providers, it throws an exception when used with the OleDb provider.
So basically Integrated Security=SSPI; is preferred since works with both SQLClient & OleDB provider.
Here's the full set of syntaxes according to MSDN - Connection String Syntax (ADO.NET)
Using Windows Authentication
To connect to the database server is recommended to use Windows Authentication, commonly known as integrated security. To specify the Windows authentication, you can use any of the following two key-value pairs with the data provider. NET Framework for SQL Server:
Integrated Security = true;
Integrated Security = SSPI;
However, only the second works with the data provider .NET Framework OleDb. If you set Integrated Security = true for ConnectionString an exception is thrown.
To specify the Windows authentication in the data provider. NET Framework for ODBC, you should use the following key-value pair.
Trusted_Connection = yes;
Source: MSDN: Working with Connection Strings
Many questions get answers if we use .Net Reflector to see the actual code of SqlConnection :)
true and sspi are the same:
internal class DbConnectionOptions
...
internal bool ConvertValueToIntegratedSecurityInternal(string stringValue)
{
if ((CompareInsensitiveInvariant(stringValue, "sspi") || CompareInsensitiveInvariant(stringValue, "true")) || CompareInsensitiveInvariant(stringValue, "yes"))
{
return true;
}
}
...
EDIT 20.02.2018
Now in .Net Core we can see its open source on github!
Search for ConvertValueToIntegratedSecurityInternal method:
https://github.com/dotnet/corefx/blob/fdbb160aeb0fad168b3603dbdd971d568151a0c8/src/System.Data.SqlClient/src/System/Data/Common/DbConnectionOptions.cs
Integrated Security = False : User ID and Password are specified in the connection.
Integrated Security = true : the current Windows account credentials are used for authentication.
Integrated Security = SSPI : this is equivalant to true.
We can avoid the username and password attributes from the connection string and use the Integrated Security
Let me start with Integrated Security = false
false User ID and Password are specified in the connection string.
true Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and SSPI.
If User ID and Password are specified and Integrated Security is set to true, then User ID and Password will be ignored and Integrated Security will be used
Note that connection strings are specific to what and how you are connecting to data. These are connecting to the same database but the first is using .NET Framework Data Provider for SQL Server. Integrated Security=True will not work for OleDb.
Data Source=.;Initial Catalog=aspnetdb;Integrated Security=True
Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb
When in doubt use the Visual Studio Server Explorer Data Connections.
What is sspi?
Connection Strings Syntax
True is only valid if you're using the .NET SqlClient library. It isn't valid when using OLEDB.
Where SSPI is bvaid in both either you are using .net SqlClient library or OLEDB.
In my point of view,
If you dont use Integrated security=SSPI,then you need to hardcode the username and password in the connection string which means "relatively insecure" why because, all the employees have the access even ex-employee could use the information maliciously.