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#.
Related
I have a database on which I have very limited access. I need to get some data from a VIEW from a database. I have a generic Windows account and I can only login if I Shift+Click on Sql Server Studio and open as different user and use the default Windows Authentication(No Sql Server Authentication Works). I created some DNSs but I can only login with the default Windows NT authentication and SQL Server. The problem is, when I use the Windows NT I get the error:
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user
'DOMAIN\SERVERNAME'. ERROR [28000]
My Code:
try
{
OdbcConnection cn;
OdbcCommand cmd;
string MyString;
MyString = "Select * from users";
cn = new OdbcConnection("dsn=DB77;UID=****;PWD=****;");
cmd = new OdbcCommand(MyString, cn);
cn.Open();
cn.Close();
}
catch(Exception e)
{
return e.ToString();
}
I want to use a Windows Authentication to get data from the db. Is it something wrong in Code, ODBC Data Source Admin, IIS?
I personally use NHibernate with MVC. Originally I picked it up because our database doesn't support EF but enjoy it enough that even if we moved to SQL Server I'd keep NHibernate.
The learning curve is kinda weird. It is definitely steep to become an expert, but it is interesting in that it is pretty organic to let it handle more and more of the work for you as you get comfortable with certain layers.
So for your case NHibernate probably supports your database, can be used as a simple data access layer (just returning DTOs), provides a database agnostic interface and can support SQL Server when the time comes. If you end up wanting more out of NHibernate it is there when the time comes.
nHibernate doesn't have anything to do with Windows Authentication. SQL server will use your process identity to authenticate
if configured to use windows authentication. What that means is that if your application is a web app, you'll need to ensure that your
application runs under the windows identity that can be authenticated by SQL Server.
And it's not possible to provide windows credentials via database connection string, unfortunately.
Example of connection string when using windows authentication:
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
Impersonation:
http://msdn.microsoft.com/en-us/library/xh507fc5.aspx
I'm trying to test SQL Server (I've never used before), and I always get this annoying error:
I've traveled multiple websites, multiple questions, and do not get the solution. The user 'MrDan' is like the WINDOWS admin... I can't understand why can't connect.
Dim con As New MySqlConnection("Server=********;Database=PruebaMultiple;User Id=MrDan;Password=*****;")
Am I missing something? Or am I using a bad connection string?
First, connections to SQL Server are made using SqlConnection, not MySqlConnection. MySQL and SQL Server are different database systems. Second, explicit credentials work only for SQL accounts. You're trying to log in with a Windows user, and should therefore use Integrated Security=SSPI in your connection string (and omit user name and password). To sum up:
Dim con As New SqlConnection("Server=********;Database=PruebaMultiple;Integrated Security=SSPI")
Trying to connect a classic asp page to sql server express 2014 using a dsn.
I've created a new database (in sql server management studio) called warehouse, with a table called users. I've added a new login called user1 with a password and mapped it to the warehouse database
I've created an ODBC System DSN on the same machine, using sql server native client 11 driver called SQLServer2 with integrated windows authentication, and a default database warehouse. Testing the connection at this point works fine!
On the ASP page I'm using:-
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "dsn=SQLServer2;uid=user1;pwd=xyz;"
Error:- Microsoft OLE DB Provider for ODBC Drivers error '80040e4d'
[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open
database "warehouse" requested by the login. The login failed.
I'm pretty sure that its a permissions issue and that the connection string is OK otherwise but after hours and hours of trying different strings, different users, I can't seem to get the damn thing to connect
If Anyone can throw any ideas my way I'd be very grateful. Thanks!
The way you are describing the problem the DSN uses integrated security so your credentials might be ignored and the database is called using the application pool identity.
If you have no specific reason why you need to use a DSN I would recommend using a connection string like the following:
objConn.Open "Provider=sqloledb;Server=yourdbserver;uid=user1;pwd=xyz;Database=warehouse"
A look into your SQL server log might give you a hint as to which user is actually failing to connect to your database.
I think you should have done something like this
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString "dsn=SQLServer2;uid=user1;pwd=xyz;"
objConn.open
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.