Can't connect to SQL Server with my vb.net program - sql-server

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")

Related

SAS connection to SQL Server DB using Windows Authentication

Can anyone give me the libname statement for making SAS to SQL Server connection using windows authentication or is it even possible without entering userid and password?
Thanks
You will want to use a connection string that has Trusted_Connection=True; How that property is communicated to the engine is dependent on the engine.
A great resource for all things connection strings is https://www.connectionstrings.com/sql-server/

ASP.NET MVC Connect to DSN with limited access

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

Difficulty Connecting To SQL Server Express 2012 from VB.NET (Express)

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#.

Excel VBA connect to SQL Server Login Error

I am a beginner in Excel VBA and faced a problem: I can connect to remote SQL Server through Management Studio, but got error when connecting through Excel VBA.
Below is my connection string and the error code.
Const connection_string As String = "Provider=SQLOLEDB.1;Persist Security Info=True;Integrated Security=SSPI;Initial Catalog=RPGTFDB;Data Source=122.xxx.xxx.xxx;User ID=sa;Password=xxx"
Error is:
Run-time error (80004005): Login failed for user ". The user is not associated with a trusted SQL Server connection.
Is it not able to get the User ID?
However when I connect to local computer, like 127.0.0.1, the above connection string works.
Thanks a lot!
In your connection string you should not use
User ID=sa;Password=xxx
As the error seems to be indicating that only Windows Logins are enabled on the SQL Server and this connection string item is only for SQL Logins.
Do you have to punch in a login and password when connecting with management studio? If not then I suggest you have a database that authenticates a connection using your windows login.
If this is the case, then remove the User ID and Password bits and insert Trusted_Connection=Yes instead.
Finally, the connection string is created using UDL application.
Despite I still cannot find any difference between the string and the manual created one, it works!
Follow the instructions in this link:
http://social.technet.microsoft.com/wiki/contents/articles/1409.how-to-create-a-sql-connection-string-for-an-application-udl-file.aspx

Connecting to SQL Server over network using ADO from Excel VBA

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.

Resources